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 * Support authenticaten of trusted domains.
913 * These flags are the minimum required set which works with win2k3
916 if (pdb_capabilities() & PDB_CAP_TRUSTED_DOMAINS_EX) {
917 srv_flgs |= NETLOGON_NEG_TRANSITIVE_TRUSTS |
918 NETLOGON_NEG_DNS_DOMAIN_TRUSTS |
919 NETLOGON_NEG_CROSS_FOREST_TRUSTS |
920 NETLOGON_NEG_NEUTRALIZE_NT4_EMULATION;
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,
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 = NULL;
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 const char *computer_name = "<unknown>";
1305 if (creds && creds->computer_name) {
1306 computer_name = creds->computer_name;
1308 DEBUG(2,("_netr_ServerPasswordSet2: netlogon_creds_server_step "
1309 "failed. Rejecting auth request from client %s machine account %s\n",
1310 r->in.computer_name, computer_name));
1315 memcpy(password_buf.data, r->in.new_password->data, 512);
1316 SIVAL(password_buf.data, 512, r->in.new_password->length);
1317 netlogon_creds_arcfour_crypt(creds, password_buf.data, 516);
1319 if (!extract_pw_from_buffer(p->mem_ctx, password_buf.data, &plaintext)) {
1321 return NT_STATUS_WRONG_PASSWORD;
1324 mdfour(nt_hash.hash, plaintext.data, plaintext.length);
1326 status = netr_set_machine_account_password(p->mem_ctx,
1329 creds->account_name,
1335 /*************************************************************************
1336 _netr_LogonSamLogoff
1337 *************************************************************************/
1339 NTSTATUS _netr_LogonSamLogoff(struct pipes_struct *p,
1340 struct netr_LogonSamLogoff *r)
1343 struct netlogon_creds_CredentialState *creds;
1346 status = netr_creds_server_step_check(p, p->mem_ctx,
1347 r->in.computer_name,
1349 r->out.return_authenticator,
1356 static NTSTATUS _netr_LogonSamLogon_check(const struct netr_LogonSamLogonEx *r)
1358 switch (r->in.logon_level) {
1359 case NetlogonInteractiveInformation:
1360 case NetlogonServiceInformation:
1361 case NetlogonInteractiveTransitiveInformation:
1362 case NetlogonServiceTransitiveInformation:
1363 if (r->in.logon->password == NULL) {
1364 return NT_STATUS_INVALID_PARAMETER;
1367 switch (r->in.validation_level) {
1368 case NetlogonValidationSamInfo: /* 2 */
1369 case NetlogonValidationSamInfo2: /* 3 */
1371 case NetlogonValidationSamInfo4: /* 6 */
1372 if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
1373 DEBUG(10,("Not adding validation info level 6 "
1374 "without ADS passdb backend\n"));
1375 return NT_STATUS_INVALID_INFO_CLASS;
1379 return NT_STATUS_INVALID_INFO_CLASS;
1383 case NetlogonNetworkInformation:
1384 case NetlogonNetworkTransitiveInformation:
1385 if (r->in.logon->network == NULL) {
1386 return NT_STATUS_INVALID_PARAMETER;
1389 switch (r->in.validation_level) {
1390 case NetlogonValidationSamInfo: /* 2 */
1391 case NetlogonValidationSamInfo2: /* 3 */
1393 case NetlogonValidationSamInfo4: /* 6 */
1394 if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
1395 DEBUG(10,("Not adding validation info level 6 "
1396 "without ADS passdb backend\n"));
1397 return NT_STATUS_INVALID_INFO_CLASS;
1401 return NT_STATUS_INVALID_INFO_CLASS;
1406 case NetlogonGenericInformation:
1407 if (r->in.logon->generic == NULL) {
1408 return NT_STATUS_INVALID_PARAMETER;
1411 /* we don't support this here */
1412 return NT_STATUS_INVALID_PARAMETER;
1414 switch (r->in.validation_level) {
1415 /* TODO: case NetlogonValidationGenericInfo: 4 */
1416 case NetlogonValidationGenericInfo2: /* 5 */
1419 return NT_STATUS_INVALID_INFO_CLASS;
1425 return NT_STATUS_INVALID_PARAMETER;
1428 return NT_STATUS_OK;
1431 /*************************************************************************
1432 _netr_LogonSamLogon_base
1433 *************************************************************************/
1435 static NTSTATUS _netr_LogonSamLogon_base(struct pipes_struct *p,
1436 struct netr_LogonSamLogonEx *r,
1437 struct netlogon_creds_CredentialState *creds)
1439 NTSTATUS status = NT_STATUS_OK;
1440 union netr_LogonLevel *logon = r->in.logon;
1441 const char *nt_username, *nt_domain, *nt_workstation;
1442 struct auth_usersupplied_info *user_info = NULL;
1443 struct auth_serversupplied_info *server_info = NULL;
1444 struct auth_context *auth_context = NULL;
1445 uint8_t pipe_session_key[16];
1446 bool process_creds = true;
1450 case NDR_NETR_LOGONSAMLOGON:
1451 process_creds = true;
1452 fn = "_netr_LogonSamLogon";
1454 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
1455 process_creds = true;
1456 fn = "_netr_LogonSamLogonWithFlags";
1458 case NDR_NETR_LOGONSAMLOGONEX:
1459 process_creds = false;
1460 fn = "_netr_LogonSamLogonEx";
1463 return NT_STATUS_INTERNAL_ERROR;
1466 *r->out.authoritative = true; /* authoritative response */
1468 switch (r->in.validation_level) {
1470 r->out.validation->sam2 = talloc_zero(p->mem_ctx, struct netr_SamInfo2);
1471 if (!r->out.validation->sam2) {
1472 return NT_STATUS_NO_MEMORY;
1476 r->out.validation->sam3 = talloc_zero(p->mem_ctx, struct netr_SamInfo3);
1477 if (!r->out.validation->sam3) {
1478 return NT_STATUS_NO_MEMORY;
1482 r->out.validation->sam6 = talloc_zero(p->mem_ctx, struct netr_SamInfo6);
1483 if (!r->out.validation->sam6) {
1484 return NT_STATUS_NO_MEMORY;
1488 DEBUG(0,("%s: bad validation_level value %d.\n",
1489 fn, (int)r->in.validation_level));
1490 return NT_STATUS_INVALID_INFO_CLASS;
1493 switch (r->in.logon_level) {
1494 case NetlogonInteractiveInformation:
1495 case NetlogonServiceInformation:
1496 case NetlogonInteractiveTransitiveInformation:
1497 case NetlogonServiceTransitiveInformation:
1498 nt_username = logon->password->identity_info.account_name.string ?
1499 logon->password->identity_info.account_name.string : "";
1500 nt_domain = logon->password->identity_info.domain_name.string ?
1501 logon->password->identity_info.domain_name.string : "";
1502 nt_workstation = logon->password->identity_info.workstation.string ?
1503 logon->password->identity_info.workstation.string : "";
1505 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
1507 case NetlogonNetworkInformation:
1508 case NetlogonNetworkTransitiveInformation:
1509 nt_username = logon->network->identity_info.account_name.string ?
1510 logon->network->identity_info.account_name.string : "";
1511 nt_domain = logon->network->identity_info.domain_name.string ?
1512 logon->network->identity_info.domain_name.string : "";
1513 nt_workstation = logon->network->identity_info.workstation.string ?
1514 logon->network->identity_info.workstation.string : "";
1516 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
1519 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1520 return NT_STATUS_INVALID_INFO_CLASS;
1523 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
1524 fstrcpy(current_user_info.smb_name, nt_username);
1525 sub_set_smb_name(nt_username);
1527 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
1528 r->in.validation_level, nt_username));
1530 status = NT_STATUS_OK;
1532 switch (r->in.logon_level) {
1533 case NetlogonNetworkInformation:
1534 case NetlogonNetworkTransitiveInformation:
1536 const char *wksname = nt_workstation;
1538 status = make_auth_context_fixed(talloc_tos(), &auth_context,
1539 logon->network->challenge);
1540 if (!NT_STATUS_IS_OK(status)) {
1544 /* For a network logon, the workstation name comes in with two
1545 * backslashes in the front. Strip them if they are there. */
1547 if (*wksname == '\\') wksname++;
1548 if (*wksname == '\\') wksname++;
1550 /* Standard challenge/response authentication */
1551 if (!make_user_info_netlogon_network(&user_info,
1552 nt_username, nt_domain,
1555 logon->network->identity_info.parameter_control,
1556 logon->network->lm.data,
1557 logon->network->lm.length,
1558 logon->network->nt.data,
1559 logon->network->nt.length)) {
1560 status = NT_STATUS_NO_MEMORY;
1564 case NetlogonInteractiveInformation:
1565 case NetlogonServiceInformation:
1566 case NetlogonInteractiveTransitiveInformation:
1567 case NetlogonServiceTransitiveInformation:
1569 /* 'Interactive' authentication, supplies the password in its
1570 MD4 form, encrypted with the session key. We will convert
1571 this to challenge/response for the auth subsystem to chew
1576 status = make_auth_context_subsystem(talloc_tos(),
1578 if (!NT_STATUS_IS_OK(status)) {
1582 auth_context->get_ntlm_challenge(auth_context, chal);
1584 if (!make_user_info_netlogon_interactive(&user_info,
1585 nt_username, nt_domain,
1588 logon->password->identity_info.parameter_control,
1590 logon->password->lmpassword.hash,
1591 logon->password->ntpassword.hash,
1592 creds->session_key)) {
1593 status = NT_STATUS_NO_MEMORY;
1598 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1599 return NT_STATUS_INVALID_INFO_CLASS;
1602 if ( NT_STATUS_IS_OK(status) ) {
1603 status = auth_context->check_ntlm_password(auth_context,
1604 user_info, &server_info);
1607 TALLOC_FREE(auth_context);
1608 free_user_info(&user_info);
1610 DEBUG(5,("%s: check_password returned status %s\n",
1611 fn, nt_errstr(status)));
1613 /* Check account and password */
1615 if (!NT_STATUS_IS_OK(status)) {
1616 /* If we don't know what this domain is, we need to
1617 indicate that we are not authoritative. This
1618 allows the client to decide if it needs to try
1619 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
1620 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1621 && !strequal(nt_domain, get_global_sam_name())
1622 && !is_trusted_domain(nt_domain) )
1623 *r->out.authoritative = false; /* We are not authoritative */
1625 TALLOC_FREE(server_info);
1629 if (server_info->guest) {
1630 /* We don't like guest domain logons... */
1631 DEBUG(5,("%s: Attempted domain logon as GUEST "
1633 TALLOC_FREE(server_info);
1634 return NT_STATUS_LOGON_FAILURE;
1637 /* This is the point at which, if the login was successful, that
1638 the SAM Local Security Authority should record that the user is
1639 logged in to the domain. */
1641 if (process_creds) {
1642 /* Get the pipe session key from the creds. */
1643 memcpy(pipe_session_key, creds->session_key, 16);
1645 struct schannel_state *schannel_auth;
1646 /* Get the pipe session key from the schannel. */
1647 if ((p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL)
1648 || (p->auth.auth_ctx == NULL)) {
1649 return NT_STATUS_INVALID_HANDLE;
1652 schannel_auth = talloc_get_type_abort(p->auth.auth_ctx,
1653 struct schannel_state);
1654 memcpy(pipe_session_key, schannel_auth->creds->session_key, 16);
1657 switch (r->in.validation_level) {
1659 status = serverinfo_to_SamInfo2(server_info, pipe_session_key, 16,
1660 r->out.validation->sam2);
1663 status = serverinfo_to_SamInfo3(server_info, pipe_session_key, 16,
1664 r->out.validation->sam3);
1667 status = serverinfo_to_SamInfo6(server_info, pipe_session_key, 16,
1668 r->out.validation->sam6);
1672 TALLOC_FREE(server_info);
1677 /****************************************************************
1678 _netr_LogonSamLogonWithFlags
1679 ****************************************************************/
1681 NTSTATUS _netr_LogonSamLogonWithFlags(struct pipes_struct *p,
1682 struct netr_LogonSamLogonWithFlags *r)
1685 struct netlogon_creds_CredentialState *creds;
1686 struct netr_LogonSamLogonEx r2;
1687 struct netr_Authenticator return_authenticator;
1689 *r->out.authoritative = true;
1691 r2.in.server_name = r->in.server_name;
1692 r2.in.computer_name = r->in.computer_name;
1693 r2.in.logon_level = r->in.logon_level;
1694 r2.in.logon = r->in.logon;
1695 r2.in.validation_level = r->in.validation_level;
1696 r2.in.flags = r->in.flags;
1697 r2.out.validation = r->out.validation;
1698 r2.out.authoritative = r->out.authoritative;
1699 r2.out.flags = r->out.flags;
1701 status = _netr_LogonSamLogon_check(&r2);
1702 if (!NT_STATUS_IS_OK(status)) {
1707 status = netr_creds_server_step_check(p, p->mem_ctx,
1708 r->in.computer_name,
1710 &return_authenticator,
1713 if (!NT_STATUS_IS_OK(status)) {
1717 status = _netr_LogonSamLogon_base(p, &r2, creds);
1719 *r->out.return_authenticator = return_authenticator;
1724 /*************************************************************************
1726 *************************************************************************/
1728 NTSTATUS _netr_LogonSamLogon(struct pipes_struct *p,
1729 struct netr_LogonSamLogon *r)
1732 struct netr_LogonSamLogonWithFlags r2;
1735 r2.in.server_name = r->in.server_name;
1736 r2.in.computer_name = r->in.computer_name;
1737 r2.in.credential = r->in.credential;
1738 r2.in.logon_level = r->in.logon_level;
1739 r2.in.logon = r->in.logon;
1740 r2.in.validation_level = r->in.validation_level;
1741 r2.in.return_authenticator = r->in.return_authenticator;
1742 r2.in.flags = &flags;
1743 r2.out.validation = r->out.validation;
1744 r2.out.authoritative = r->out.authoritative;
1745 r2.out.flags = &flags;
1746 r2.out.return_authenticator = r->out.return_authenticator;
1748 status = _netr_LogonSamLogonWithFlags(p, &r2);
1753 /*************************************************************************
1754 _netr_LogonSamLogonEx
1755 - no credential chaining. Map into net sam logon.
1756 *************************************************************************/
1758 NTSTATUS _netr_LogonSamLogonEx(struct pipes_struct *p,
1759 struct netr_LogonSamLogonEx *r)
1762 struct netlogon_creds_CredentialState *creds = NULL;
1763 struct loadparm_context *lp_ctx;
1765 *r->out.authoritative = true;
1767 status = _netr_LogonSamLogon_check(r);
1768 if (!NT_STATUS_IS_OK(status)) {
1772 /* Only allow this if the pipe is protected. */
1773 if (p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
1774 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1775 get_remote_machine_name() ));
1776 return NT_STATUS_INVALID_PARAMETER;
1779 lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_context());
1780 if (lp_ctx == NULL) {
1781 DEBUG(0, ("loadparm_init_s3 failed\n"));
1782 return NT_STATUS_INTERNAL_ERROR;
1786 status = schannel_get_creds_state(p->mem_ctx, lp_ctx,
1787 r->in.computer_name, &creds);
1789 talloc_unlink(p->mem_ctx, lp_ctx);
1791 if (!NT_STATUS_IS_OK(status)) {
1795 status = _netr_LogonSamLogon_base(p, r, creds);
1801 /*************************************************************************
1803 *************************************************************************/
1804 #if 0 /* JERRY -- not correct */
1805 NTSTATUS _ds_enum_dom_trusts(struct pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1806 DS_R_ENUM_DOM_TRUSTS *r_u)
1808 NTSTATUS status = NT_STATUS_OK;
1810 /* TODO: According to MSDN, the can only be executed against a
1811 DC or domain member running Windows 2000 or later. Need
1812 to test against a standalone 2k server and see what it
1813 does. A windows 2000 DC includes its own domain in the
1821 /****************************************************************
1822 ****************************************************************/
1824 WERROR _netr_LogonUasLogon(struct pipes_struct *p,
1825 struct netr_LogonUasLogon *r)
1827 p->rng_fault_state = true;
1828 return WERR_NOT_SUPPORTED;
1831 /****************************************************************
1832 ****************************************************************/
1834 WERROR _netr_LogonUasLogoff(struct pipes_struct *p,
1835 struct netr_LogonUasLogoff *r)
1837 p->rng_fault_state = true;
1838 return WERR_NOT_SUPPORTED;
1841 /****************************************************************
1842 ****************************************************************/
1844 NTSTATUS _netr_DatabaseDeltas(struct pipes_struct *p,
1845 struct netr_DatabaseDeltas *r)
1847 p->rng_fault_state = true;
1848 return NT_STATUS_NOT_IMPLEMENTED;
1851 /****************************************************************
1852 ****************************************************************/
1854 NTSTATUS _netr_DatabaseSync(struct pipes_struct *p,
1855 struct netr_DatabaseSync *r)
1857 p->rng_fault_state = true;
1858 return NT_STATUS_NOT_IMPLEMENTED;
1861 /****************************************************************
1862 ****************************************************************/
1864 NTSTATUS _netr_AccountDeltas(struct pipes_struct *p,
1865 struct netr_AccountDeltas *r)
1867 p->rng_fault_state = true;
1868 return NT_STATUS_NOT_IMPLEMENTED;
1871 /****************************************************************
1872 ****************************************************************/
1874 NTSTATUS _netr_AccountSync(struct pipes_struct *p,
1875 struct netr_AccountSync *r)
1877 p->rng_fault_state = true;
1878 return NT_STATUS_NOT_IMPLEMENTED;
1881 /****************************************************************
1882 ****************************************************************/
1884 static bool wb_getdcname(TALLOC_CTX *mem_ctx,
1886 const char **dcname,
1891 struct wbcDomainControllerInfo *dc_info = NULL;
1893 result = wbcLookupDomainController(domain,
1897 case WBC_ERR_SUCCESS:
1899 case WBC_ERR_WINBIND_NOT_AVAILABLE:
1901 case WBC_ERR_DOMAIN_NOT_FOUND:
1902 *werr = WERR_NO_SUCH_DOMAIN;
1905 *werr = WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1909 *dcname = talloc_strdup(mem_ctx, dc_info->dc_name);
1910 wbcFreeMemory(dc_info);
1921 /****************************************************************
1923 ****************************************************************/
1925 WERROR _netr_GetDcName(struct pipes_struct *p,
1926 struct netr_GetDcName *r)
1931 struct netr_DsRGetDCNameInfo *info;
1934 ret = wb_getdcname(p->mem_ctx,
1937 WBC_LOOKUP_DC_IS_FLAT_NAME |
1938 WBC_LOOKUP_DC_RETURN_FLAT_NAME |
1939 WBC_LOOKUP_DC_PDC_REQUIRED,
1945 flags = DS_PDC_REQUIRED | DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1947 status = dsgetdcname(p->mem_ctx,
1954 if (!NT_STATUS_IS_OK(status)) {
1955 return ntstatus_to_werror(status);
1958 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1960 if (!*r->out.dcname) {
1967 /****************************************************************
1969 ****************************************************************/
1971 WERROR _netr_GetAnyDCName(struct pipes_struct *p,
1972 struct netr_GetAnyDCName *r)
1977 struct netr_DsRGetDCNameInfo *info;
1980 ret = wb_getdcname(p->mem_ctx,
1983 WBC_LOOKUP_DC_IS_FLAT_NAME |
1984 WBC_LOOKUP_DC_RETURN_FLAT_NAME,
1990 flags = DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1992 status = dsgetdcname(p->mem_ctx,
1999 if (!NT_STATUS_IS_OK(status)) {
2000 return ntstatus_to_werror(status);
2003 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
2005 if (!*r->out.dcname) {
2012 /****************************************************************
2013 ****************************************************************/
2015 NTSTATUS _netr_DatabaseSync2(struct pipes_struct *p,
2016 struct netr_DatabaseSync2 *r)
2018 p->rng_fault_state = true;
2019 return NT_STATUS_NOT_IMPLEMENTED;
2022 /****************************************************************
2023 ****************************************************************/
2025 NTSTATUS _netr_DatabaseRedo(struct pipes_struct *p,
2026 struct netr_DatabaseRedo *r)
2028 p->rng_fault_state = true;
2029 return NT_STATUS_NOT_IMPLEMENTED;
2032 /****************************************************************
2033 ****************************************************************/
2035 WERROR _netr_DsRGetDCName(struct pipes_struct *p,
2036 struct netr_DsRGetDCName *r)
2038 p->rng_fault_state = true;
2039 return WERR_NOT_SUPPORTED;
2042 /****************************************************************
2043 ****************************************************************/
2045 NTSTATUS _netr_LogonGetCapabilities(struct pipes_struct *p,
2046 struct netr_LogonGetCapabilities *r)
2048 struct netlogon_creds_CredentialState *creds;
2052 status = netr_creds_server_step_check(p, p->mem_ctx,
2053 r->in.computer_name,
2055 r->out.return_authenticator,
2058 if (!NT_STATUS_IS_OK(status)) {
2062 if (r->in.query_level != 1) {
2063 return NT_STATUS_NOT_SUPPORTED;
2066 r->out.capabilities->server_capabilities = creds->negotiate_flags;
2068 return NT_STATUS_OK;
2071 /****************************************************************
2072 ****************************************************************/
2074 WERROR _netr_NETRLOGONSETSERVICEBITS(struct pipes_struct *p,
2075 struct netr_NETRLOGONSETSERVICEBITS *r)
2077 p->rng_fault_state = true;
2078 return WERR_NOT_SUPPORTED;
2081 /****************************************************************
2082 ****************************************************************/
2084 WERROR _netr_LogonGetTrustRid(struct pipes_struct *p,
2085 struct netr_LogonGetTrustRid *r)
2087 p->rng_fault_state = true;
2088 return WERR_NOT_SUPPORTED;
2091 /****************************************************************
2092 ****************************************************************/
2094 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(struct pipes_struct *p,
2095 struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
2097 p->rng_fault_state = true;
2098 return WERR_NOT_SUPPORTED;
2101 /****************************************************************
2102 ****************************************************************/
2104 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(struct pipes_struct *p,
2105 struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
2107 p->rng_fault_state = true;
2108 return WERR_NOT_SUPPORTED;
2111 /****************************************************************
2112 ****************************************************************/
2114 WERROR _netr_DsRGetDCNameEx(struct pipes_struct *p,
2115 struct netr_DsRGetDCNameEx *r)
2117 p->rng_fault_state = true;
2118 return WERR_NOT_SUPPORTED;
2121 /****************************************************************
2122 ****************************************************************/
2124 WERROR _netr_DsRGetSiteName(struct pipes_struct *p,
2125 struct netr_DsRGetSiteName *r)
2127 p->rng_fault_state = true;
2128 return WERR_NOT_SUPPORTED;
2131 /****************************************************************
2132 ****************************************************************/
2134 NTSTATUS _netr_LogonGetDomainInfo(struct pipes_struct *p,
2135 struct netr_LogonGetDomainInfo *r)
2137 p->rng_fault_state = true;
2138 return NT_STATUS_NOT_IMPLEMENTED;
2141 /****************************************************************
2142 ****************************************************************/
2144 WERROR _netr_ServerPasswordGet(struct pipes_struct *p,
2145 struct netr_ServerPasswordGet *r)
2147 p->rng_fault_state = true;
2148 return WERR_NOT_SUPPORTED;
2151 /****************************************************************
2152 ****************************************************************/
2154 WERROR _netr_NETRLOGONSENDTOSAM(struct pipes_struct *p,
2155 struct netr_NETRLOGONSENDTOSAM *r)
2157 p->rng_fault_state = true;
2158 return WERR_NOT_SUPPORTED;
2161 /****************************************************************
2162 ****************************************************************/
2164 WERROR _netr_DsRAddressToSitenamesW(struct pipes_struct *p,
2165 struct netr_DsRAddressToSitenamesW *r)
2167 p->rng_fault_state = true;
2168 return WERR_NOT_SUPPORTED;
2171 /****************************************************************
2172 ****************************************************************/
2174 WERROR _netr_DsRGetDCNameEx2(struct pipes_struct *p,
2175 struct netr_DsRGetDCNameEx2 *r)
2177 p->rng_fault_state = true;
2178 return WERR_NOT_SUPPORTED;
2181 /****************************************************************
2182 ****************************************************************/
2184 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(struct pipes_struct *p,
2185 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
2187 p->rng_fault_state = true;
2188 return WERR_NOT_SUPPORTED;
2191 /****************************************************************
2192 ****************************************************************/
2194 WERROR _netr_NetrEnumerateTrustedDomainsEx(struct pipes_struct *p,
2195 struct netr_NetrEnumerateTrustedDomainsEx *r)
2197 p->rng_fault_state = true;
2198 return WERR_NOT_SUPPORTED;
2201 /****************************************************************
2202 ****************************************************************/
2204 WERROR _netr_DsRAddressToSitenamesExW(struct pipes_struct *p,
2205 struct netr_DsRAddressToSitenamesExW *r)
2207 p->rng_fault_state = true;
2208 return WERR_NOT_SUPPORTED;
2211 /****************************************************************
2212 ****************************************************************/
2214 WERROR _netr_DsrGetDcSiteCoverageW(struct pipes_struct *p,
2215 struct netr_DsrGetDcSiteCoverageW *r)
2217 p->rng_fault_state = true;
2218 return WERR_NOT_SUPPORTED;
2221 /****************************************************************
2222 ****************************************************************/
2224 WERROR _netr_DsrEnumerateDomainTrusts(struct pipes_struct *p,
2225 struct netr_DsrEnumerateDomainTrusts *r)
2227 p->rng_fault_state = true;
2228 return WERR_NOT_SUPPORTED;
2231 /****************************************************************
2232 ****************************************************************/
2234 WERROR _netr_DsrDeregisterDNSHostRecords(struct pipes_struct *p,
2235 struct netr_DsrDeregisterDNSHostRecords *r)
2237 p->rng_fault_state = true;
2238 return WERR_NOT_SUPPORTED;
2241 /****************************************************************
2242 ****************************************************************/
2244 NTSTATUS _netr_ServerTrustPasswordsGet(struct pipes_struct *p,
2245 struct netr_ServerTrustPasswordsGet *r)
2247 p->rng_fault_state = true;
2248 return NT_STATUS_NOT_IMPLEMENTED;
2251 /****************************************************************
2252 ****************************************************************/
2254 WERROR _netr_DsRGetForestTrustInformation(struct pipes_struct *p,
2255 struct netr_DsRGetForestTrustInformation *r)
2257 p->rng_fault_state = true;
2258 return WERR_NOT_SUPPORTED;
2261 /****************************************************************
2262 ****************************************************************/
2264 static NTSTATUS fill_forest_trust_array(TALLOC_CTX *mem_ctx,
2265 struct lsa_ForestTrustInformation *info)
2267 struct lsa_ForestTrustRecord *e;
2268 struct pdb_domain_info *dom_info;
2269 struct lsa_ForestTrustDomainInfo *domain_info;
2271 dom_info = pdb_get_domain_info(mem_ctx);
2272 if (dom_info == NULL) {
2273 return NT_STATUS_NO_MEMORY;
2277 info->entries = talloc_array(info, struct lsa_ForestTrustRecord *, 2);
2278 if (info->entries == NULL) {
2279 return NT_STATUS_NO_MEMORY;
2282 e = talloc(info, struct lsa_ForestTrustRecord);
2284 return NT_STATUS_NO_MEMORY;
2288 e->type = LSA_FOREST_TRUST_TOP_LEVEL_NAME;
2289 e->time = 0; /* so far always 0 in trces. */
2290 e->forest_trust_data.top_level_name.string = talloc_steal(info,
2291 dom_info->dns_forest);
2293 info->entries[0] = e;
2295 e = talloc(info, struct lsa_ForestTrustRecord);
2297 return NT_STATUS_NO_MEMORY;
2300 /* TODO: check if disabled and set flags accordingly */
2302 e->type = LSA_FOREST_TRUST_DOMAIN_INFO;
2303 e->time = 0; /* so far always 0 in traces. */
2305 domain_info = &e->forest_trust_data.domain_info;
2306 domain_info->domain_sid = dom_sid_dup(info, &dom_info->sid);
2308 domain_info->dns_domain_name.string = talloc_steal(info,
2309 dom_info->dns_domain);
2310 domain_info->netbios_domain_name.string = talloc_steal(info,
2313 info->entries[1] = e;
2315 return NT_STATUS_OK;
2318 /****************************************************************
2319 _netr_GetForestTrustInformation
2320 ****************************************************************/
2322 NTSTATUS _netr_GetForestTrustInformation(struct pipes_struct *p,
2323 struct netr_GetForestTrustInformation *r)
2326 struct netlogon_creds_CredentialState *creds;
2327 struct lsa_ForestTrustInformation *info, **info_ptr;
2328 struct loadparm_context *lp_ctx;
2330 /* TODO: check server name */
2332 lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_context());
2333 if (lp_ctx == NULL) {
2334 DEBUG(0, ("loadparm_init_s3 failed\n"));
2335 return NT_STATUS_INTERNAL_ERROR;
2338 status = schannel_check_creds_state(p->mem_ctx, lp_ctx,
2339 r->in.computer_name,
2341 r->out.return_authenticator,
2343 talloc_unlink(p->mem_ctx, lp_ctx);
2344 if (!NT_STATUS_IS_OK(status)) {
2348 if ((creds->secure_channel_type != SEC_CHAN_DNS_DOMAIN) &&
2349 (creds->secure_channel_type != SEC_CHAN_DOMAIN)) {
2350 return NT_STATUS_NOT_IMPLEMENTED;
2353 info_ptr = talloc(p->mem_ctx, struct lsa_ForestTrustInformation *);
2355 return NT_STATUS_NO_MEMORY;
2357 info = talloc_zero(info_ptr, struct lsa_ForestTrustInformation);
2359 return NT_STATUS_NO_MEMORY;
2362 status = fill_forest_trust_array(p->mem_ctx, info);
2363 if (!NT_STATUS_IS_OK(status)) {
2368 r->out.forest_trust_info = info_ptr;
2370 return NT_STATUS_OK;
2373 /****************************************************************
2374 ****************************************************************/
2376 static NTSTATUS get_password_from_trustAuth(TALLOC_CTX *mem_ctx,
2377 const DATA_BLOB *trustAuth_blob,
2378 const DATA_BLOB *session_key,
2379 struct samr_Password *current_pw_enc,
2380 struct samr_Password *previous_pw_enc)
2382 enum ndr_err_code ndr_err;
2383 struct trustAuthInOutBlob trustAuth;
2385 ndr_err = ndr_pull_struct_blob_all(trustAuth_blob, mem_ctx, &trustAuth,
2386 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2387 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2388 return NT_STATUS_UNSUCCESSFUL;
2392 if (trustAuth.count != 0 && trustAuth.current.count != 0 &&
2393 trustAuth.current.array[0].AuthType == TRUST_AUTH_TYPE_CLEAR) {
2394 mdfour(previous_pw_enc->hash,
2395 trustAuth.current.array[0].AuthInfo.clear.password,
2396 trustAuth.current.array[0].AuthInfo.clear.size);
2398 return NT_STATUS_UNSUCCESSFUL;
2401 arcfour_crypt_blob(current_pw_enc->hash, sizeof(current_pw_enc->hash),
2404 if (trustAuth.previous.count != 0 &&
2405 trustAuth.previous.array[0].AuthType == TRUST_AUTH_TYPE_CLEAR) {
2406 mdfour(previous_pw_enc->hash,
2407 trustAuth.previous.array[0].AuthInfo.clear.password,
2408 trustAuth.previous.array[0].AuthInfo.clear.size);
2410 mdfour(previous_pw_enc->hash, NULL, 0);
2412 arcfour_crypt_blob(previous_pw_enc->hash, sizeof(previous_pw_enc->hash),
2415 return NT_STATUS_OK;
2418 /****************************************************************
2419 _netr_ServerGetTrustInfo
2420 ****************************************************************/
2422 NTSTATUS _netr_ServerGetTrustInfo(struct pipes_struct *p,
2423 struct netr_ServerGetTrustInfo *r)
2426 struct netlogon_creds_CredentialState *creds;
2428 size_t account_name_last;
2430 struct netr_TrustInfo *trust_info;
2431 struct pdb_trusted_domain *td;
2432 DATA_BLOB trustAuth_blob;
2433 struct samr_Password *new_owf_enc;
2434 struct samr_Password *old_owf_enc;
2435 DATA_BLOB session_key;
2436 struct loadparm_context *lp_ctx;
2438 lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_context());
2439 if (lp_ctx == NULL) {
2440 DEBUG(0, ("loadparm_init_s3 failed\n"));
2441 return NT_STATUS_INTERNAL_ERROR;
2444 /* TODO: check server name */
2446 status = schannel_check_creds_state(p->mem_ctx, lp_ctx,
2447 r->in.computer_name,
2449 r->out.return_authenticator,
2451 talloc_unlink(p->mem_ctx, lp_ctx);
2452 if (!NT_STATUS_IS_OK(status)) {
2456 account_name = talloc_strdup(p->mem_ctx, r->in.account_name);
2457 if (account_name == NULL) {
2458 return NT_STATUS_NO_MEMORY;
2461 account_name_last = strlen(account_name);
2462 if (account_name_last == 0) {
2463 return NT_STATUS_INVALID_PARAMETER;
2465 account_name_last--;
2466 if (account_name[account_name_last] == '.') {
2467 account_name[account_name_last] = '\0';
2470 if ((creds->secure_channel_type != SEC_CHAN_DNS_DOMAIN) &&
2471 (creds->secure_channel_type != SEC_CHAN_DOMAIN)) {
2479 account_name_last = strlen(account_name);
2480 if (account_name_last == 0) {
2481 return NT_STATUS_INVALID_PARAMETER;
2483 account_name_last--;
2484 if (account_name[account_name_last] == '$') {
2485 account_name[account_name_last] = '\0';
2488 status = pdb_get_trusted_domain(p->mem_ctx, account_name, &td);
2489 if (!NT_STATUS_IS_OK(status)) {
2493 if (r->out.trust_info != NULL) {
2494 trust_info = talloc_zero(p->mem_ctx, struct netr_TrustInfo);
2495 if (trust_info == NULL) {
2496 return NT_STATUS_NO_MEMORY;
2498 trust_info->count = 1;
2500 trust_info->data = talloc_array(trust_info, uint32_t, 1);
2501 if (trust_info->data == NULL) {
2502 return NT_STATUS_NO_MEMORY;
2504 trust_info->data[0] = td->trust_attributes;
2506 *r->out.trust_info = trust_info;
2509 new_owf_enc = talloc_zero(p->mem_ctx, struct samr_Password);
2510 old_owf_enc = talloc_zero(p->mem_ctx, struct samr_Password);
2511 if (new_owf_enc == NULL || old_owf_enc == NULL) {
2512 return NT_STATUS_NO_MEMORY;
2515 /* TODO: which trustAuth shall we use if we have in/out trust or do they have to
2517 if (td->trust_direction & NETR_TRUST_FLAG_INBOUND) {
2518 trustAuth_blob = td->trust_auth_incoming;
2519 } else if (td->trust_direction & NETR_TRUST_FLAG_OUTBOUND) {
2520 trustAuth_blob = td->trust_auth_outgoing;
2523 session_key.data = creds->session_key;
2524 session_key.length = sizeof(creds->session_key);
2525 status = get_password_from_trustAuth(p->mem_ctx, &trustAuth_blob,
2527 new_owf_enc, old_owf_enc);
2529 if (!NT_STATUS_IS_OK(status)) {
2533 r->out.new_owf_password = new_owf_enc;
2534 r->out.old_owf_password = old_owf_enc;
2536 /* TODO: look for machine password */
2537 r->out.new_owf_password = NULL;
2538 r->out.old_owf_password = NULL;
2540 return NT_STATUS_NOT_IMPLEMENTED;
2543 return NT_STATUS_OK;
2546 /****************************************************************
2547 ****************************************************************/
2549 NTSTATUS _netr_Unused47(struct pipes_struct *p,
2550 struct netr_Unused47 *r)
2552 p->rng_fault_state = true;
2553 return NT_STATUS_NOT_IMPLEMENTED;
2556 /****************************************************************
2557 ****************************************************************/
2559 NTSTATUS _netr_DsrUpdateReadOnlyServerDnsRecords(struct pipes_struct *p,
2560 struct netr_DsrUpdateReadOnlyServerDnsRecords *r)
2562 p->rng_fault_state = true;
2563 return NT_STATUS_NOT_IMPLEMENTED;