s3-auth: rpc_server needs auth.h
[Samba/vl.git] / source3 / rpc_server / netlogon / srv_netlog_nt.c
blobdad4fcbdc7a4a4bd41a4ec1d4feff68845b92c88
1 /*
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. */
27 #include "includes.h"
28 #include "../libcli/auth/schannel.h"
29 #include "../librpc/gen_ndr/srv_netlogon.h"
30 #include "../librpc/gen_ndr/srv_samr.h"
31 #include "../librpc/gen_ndr/srv_lsa.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 "librpc/gen_ndr/messaging.h"
36 #include "../lib/crypto/md4.h"
37 #include "rpc_client/init_lsa.h"
38 #include "rpc_server/rpc_ncacn_np.h"
39 #include "../libcli/security/security.h"
40 #include "../libcli/security/dom_sid.h"
41 #include "librpc/gen_ndr/ndr_drsblobs.h"
42 #include "lib/crypto/arcfour.h"
43 #include "lib/crypto/md4.h"
44 #include "nsswitch/libwbclient/wbclient.h"
45 #include "../libcli/registry/util_reg.h"
46 #include "passdb.h"
47 #include "auth.h"
49 extern userdom_struct current_user_info;
51 #undef DBGC_CLASS
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 /*************************************************************************
60 _netr_LogonControl
61 *************************************************************************/
63 WERROR _netr_LogonControl(struct pipes_struct *p,
64 struct netr_LogonControl *r)
66 struct netr_LogonControl2Ex l;
68 switch (r->in.level) {
69 case 1:
70 break;
71 case 2:
72 return WERR_NOT_SUPPORTED;
73 default:
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;
80 l.in.data = NULL;
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 /*************************************************************************
97 _netr_LogonControl2
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)
119 wbcErr result;
120 struct wbcAuthErrorInfo *error = NULL;
122 result = wbcChangeTrustCredentials(domain, &error);
123 switch (result) {
124 case WBC_ERR_WINBIND_NOT_AVAILABLE:
125 return false;
126 case WBC_ERR_DOMAIN_NOT_FOUND:
127 *tc_status = WERR_NO_SUCH_DOMAIN;
128 return true;
129 case WBC_ERR_SUCCESS:
130 *tc_status = WERR_OK;
131 return true;
132 default:
133 break;
136 if (error && error->nt_status != 0) {
137 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
138 } else {
139 *tc_status = WERR_TRUST_FAILURE;
141 wbcFreeMemory(error);
142 return true;
145 /*************************************************************************
146 *************************************************************************/
148 static bool wb_check_trust_creds(const char *domain, WERROR *tc_status)
150 wbcErr result;
151 struct wbcAuthErrorInfo *error = NULL;
153 result = wbcCheckTrustCredentials(domain, &error);
154 switch (result) {
155 case WBC_ERR_WINBIND_NOT_AVAILABLE:
156 return false;
157 case WBC_ERR_DOMAIN_NOT_FOUND:
158 *tc_status = WERR_NO_SUCH_DOMAIN;
159 return true;
160 case WBC_ERR_SUCCESS:
161 *tc_status = WERR_OK;
162 return true;
163 default:
164 break;
167 if (error && error->nt_status != 0) {
168 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
169 } else {
170 *tc_status = WERR_TRUST_FAILURE;
172 wbcFreeMemory(error);
173 return true;
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;
186 WERROR tc_status;
187 fstring dc_name2;
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;
195 const char *fn;
196 uint32_t acct_ctrl;
198 switch (p->opnum) {
199 case NDR_NETR_LOGONCONTROL:
200 fn = "_netr_LogonControl";
201 break;
202 case NDR_NETR_LOGONCONTROL2:
203 fn = "_netr_LogonControl2";
204 break;
205 case NDR_NETR_LOGONCONTROL2EX:
206 fn = "_netr_LogonControl2Ex";
207 break;
208 default:
209 return WERR_INVALID_PARAM;
212 acct_ctrl = p->session_info->info3->base.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;
224 break;
225 default:
226 break;
229 tc_status = WERR_NO_SUCH_DOMAIN;
231 switch (r->in.function_code) {
232 case NETLOGON_CONTROL_QUERY:
233 tc_status = WERR_OK;
234 break;
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;
244 } else {
245 return WERR_ACCESS_DENIED;
247 case NETLOGON_CONTROL_TRUNCATE_LOG:
248 if (acct_ctrl & ACB_NORMAL) {
249 break;
250 } else if (acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST)) {
251 return WERR_ACCESS_DENIED;
252 } else {
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;
264 break;
265 case NETLOGON_CONTROL_SET_DBFLAG:
266 if (!r->in.data) {
267 return WERR_NOT_SUPPORTED;
269 break;
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;
278 break;
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)) {
287 break;
290 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
291 tc_status = WERR_NO_LOGON_SERVERS;
292 break;
295 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
296 if (!dc_name) {
297 return WERR_NOMEM;
300 tc_status = WERR_OK;
302 break;
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)) {
312 break;
315 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
316 tc_status = WERR_NO_LOGON_SERVERS;
317 break;
320 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
321 if (!dc_name) {
322 return WERR_NOMEM;
325 tc_status = WERR_OK;
327 break;
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;
337 break;
339 default:
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) {
349 case 1:
350 info1 = TALLOC_ZERO_P(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;
357 break;
358 case 2:
359 info2 = TALLOC_ZERO_P(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;
368 break;
369 case 3:
370 info3 = TALLOC_ZERO_P(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;
377 break;
378 case 4:
379 info4 = TALLOC_ZERO_P(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;
386 break;
387 default:
388 return WERR_UNKNOWN_LEVEL;
391 if (lp_server_role() == ROLE_DOMAIN_BDC) {
392 send_sync_message(p->msg_ctx);
395 return WERR_OK;
398 /*************************************************************************
399 _netr_NetrEnumerateTrustedDomains
400 *************************************************************************/
402 NTSTATUS _netr_NetrEnumerateTrustedDomains(struct pipes_struct *p,
403 struct netr_NetrEnumerateTrustedDomains *r)
405 NTSTATUS status;
406 NTSTATUS result = NT_STATUS_OK;
407 DATA_BLOB blob;
408 int num_domains = 0;
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;
414 int i;
415 uint32_t max_size = (uint32_t)-1;
417 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
419 status = rpcint_binding_handle(p->mem_ctx,
420 &ndr_table_lsarpc,
421 p->client_id,
422 p->session_info,
423 p->msg_ctx,
424 &h);
425 if (!NT_STATUS_IS_OK(status)) {
426 return status;
429 status = dcerpc_lsa_open_policy2(h,
430 p->mem_ctx,
431 NULL,
432 true,
433 LSA_POLICY_VIEW_LOCAL_INFORMATION,
434 &pol,
435 &result);
436 if (!NT_STATUS_IS_OK(status)) {
437 goto out;
439 if (!NT_STATUS_IS_OK(result)) {
440 status = result;
441 goto out;
444 do {
445 /* Lookup list of trusted domains */
446 status = dcerpc_lsa_EnumTrustDom(h,
447 p->mem_ctx,
448 &pol,
449 &enum_ctx,
450 &domain_list,
451 max_size,
452 &result);
453 if (!NT_STATUS_IS_OK(status)) {
454 goto out;
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)) {
459 status = result;
460 goto out;
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;
467 goto out;
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;
477 goto out;
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;
486 goto out;
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;
496 out:
497 if (is_valid_policy_hnd(&pol)) {
498 dcerpc_lsa_Close(h, p->mem_ctx, &pol, &result);
501 return status;
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)
515 NTSTATUS status;
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;
523 uint32_t rid;
525 status = dcerpc_samr_Connect2(b, mem_ctx,
526 global_myname(),
527 SAMR_ACCESS_CONNECT_TO_SERVER |
528 SAMR_ACCESS_ENUM_DOMAINS |
529 SAMR_ACCESS_LOOKUP_DOMAIN,
530 &connect_handle,
531 &result);
532 if (!NT_STATUS_IS_OK(status)) {
533 goto out;
535 if (!NT_STATUS_IS_OK(result)) {
536 status = result;
537 goto out;
540 init_lsa_String(&domain_name, get_global_sam_name());
542 status = dcerpc_samr_LookupDomain(b, mem_ctx,
543 &connect_handle,
544 &domain_name,
545 &domain_sid,
546 &result);
547 if (!NT_STATUS_IS_OK(status)) {
548 goto out;
550 if (!NT_STATUS_IS_OK(result)) {
551 status = result;
552 goto out;
555 status = dcerpc_samr_OpenDomain(b, mem_ctx,
556 &connect_handle,
557 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
558 domain_sid,
559 &domain_handle,
560 &result);
561 if (!NT_STATUS_IS_OK(status)) {
562 goto out;
564 if (!NT_STATUS_IS_OK(result)) {
565 status = result;
566 goto out;
569 init_lsa_String(&names, account_name);
571 status = dcerpc_samr_LookupNames(b, mem_ctx,
572 &domain_handle,
574 &names,
575 &rids,
576 &types,
577 &result);
578 if (!NT_STATUS_IS_OK(status)) {
579 goto out;
581 if (!NT_STATUS_IS_OK(result)) {
582 status = result;
583 goto out;
586 if (rids.count != 1) {
587 status = NT_STATUS_NO_SUCH_USER;
588 goto out;
590 if (rids.count != types.count) {
591 status = NT_STATUS_INVALID_PARAMETER;
592 goto out;
594 if (types.ids[0] != SID_NAME_USER) {
595 status = NT_STATUS_NO_SUCH_USER;
596 goto out;
599 rid = rids.ids[0];
601 status = dcerpc_samr_OpenUser(b, mem_ctx,
602 &domain_handle,
603 access_mask,
604 rid,
605 user_handle,
606 &result);
607 if (!NT_STATUS_IS_OK(status)) {
608 goto out;
610 if (!NT_STATUS_IS_OK(result)) {
611 status = result;
612 goto out;
615 if (user_rid_p) {
616 *user_rid_p = rid;
619 if (domain_sid_p) {
620 *domain_sid_p = domain_sid;
623 out:
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);
631 return status;
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,
640 struct dom_sid *sid,
641 struct messaging_context *msg_ctx)
643 NTSTATUS status;
644 NTSTATUS result = NT_STATUS_OK;
645 TALLOC_CTX *mem_ctx;
646 struct dcerpc_binding_handle *h = NULL;
647 static struct client_address client_id;
648 struct policy_handle user_handle;
649 uint32_t user_rid;
650 struct dom_sid *domain_sid;
651 uint32_t acct_ctrl;
652 union samr_UserInfo *info;
653 struct auth_serversupplied_info *session_info;
654 #if 0
657 * Currently this code is redundent as we already have a filter
658 * by hostname list. What this code really needs to do is to
659 * get a hosts allowed/hosts denied list from the SAM database
660 * on a per user basis, and make the access decision there.
661 * I will leave this code here for now as a reminder to implement
662 * this at a later date. JRA.
665 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
666 p->client_id.name,
667 p->client_id.addr)) {
668 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
669 return False;
671 #endif /* 0 */
673 mem_ctx = talloc_stackframe();
674 if (mem_ctx == NULL) {
675 status = NT_STATUS_NO_MEMORY;
676 goto out;
679 status = make_session_info_system(mem_ctx, &session_info);
680 if (!NT_STATUS_IS_OK(status)) {
681 goto out;
684 ZERO_STRUCT(user_handle);
686 strlcpy(client_id.addr, "127.0.0.1", sizeof(client_id.addr));
687 client_id.name = "127.0.0.1";
689 status = rpcint_binding_handle(mem_ctx,
690 &ndr_table_samr,
691 &client_id,
692 session_info,
693 msg_ctx,
694 &h);
695 if (!NT_STATUS_IS_OK(status)) {
696 goto out;
699 become_root();
700 status = samr_find_machine_account(mem_ctx, h, mach_acct,
701 SEC_FLAG_MAXIMUM_ALLOWED,
702 &domain_sid, &user_rid,
703 &user_handle);
704 unbecome_root();
705 if (!NT_STATUS_IS_OK(status)) {
706 goto out;
709 status = dcerpc_samr_QueryUserInfo2(h,
710 mem_ctx,
711 &user_handle,
712 UserControlInformation,
713 &info,
714 &result);
715 if (!NT_STATUS_IS_OK(status)) {
716 goto out;
718 if (!NT_STATUS_IS_OK(result)) {
719 status = result;
720 goto out;
723 acct_ctrl = info->info16.acct_flags;
725 if (acct_ctrl & ACB_DISABLED) {
726 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
727 status = NT_STATUS_ACCOUNT_DISABLED;
728 goto out;
731 if (!(acct_ctrl & ACB_SVRTRUST) &&
732 !(acct_ctrl & ACB_WSTRUST) &&
733 !(acct_ctrl & ACB_DOMTRUST))
735 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
736 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
737 goto out;
740 switch (sec_chan_type) {
741 case SEC_CHAN_BDC:
742 if (!(acct_ctrl & ACB_SVRTRUST)) {
743 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
744 "but not a server trust account\n", mach_acct));
745 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
746 goto out;
748 break;
749 case SEC_CHAN_WKSTA:
750 if (!(acct_ctrl & ACB_WSTRUST)) {
751 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
752 "but not a workstation trust account\n", mach_acct));
753 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
754 goto out;
756 break;
757 case SEC_CHAN_DOMAIN:
758 if (!(acct_ctrl & ACB_DOMTRUST)) {
759 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
760 "but not a interdomain trust account\n", mach_acct));
761 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
762 goto out;
764 break;
765 default:
766 break;
769 become_root();
770 status = dcerpc_samr_QueryUserInfo2(h,
771 mem_ctx,
772 &user_handle,
773 UserInternal1Information,
774 &info,
775 &result);
776 unbecome_root();
777 if (!NT_STATUS_IS_OK(status)) {
778 goto out;
780 if (!NT_STATUS_IS_OK(result)) {
781 status = result;
782 goto out;
785 if (info->info18.nt_pwd_active == 0) {
786 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
787 status = NT_STATUS_LOGON_FAILURE;
788 goto out;
791 /* samr gives out nthash unencrypted (!) */
792 memcpy(md4pw->hash, info->info18.nt_pwd.hash, 16);
794 sid_compose(sid, domain_sid, user_rid);
796 out:
797 if (h && is_valid_policy_hnd(&user_handle)) {
798 dcerpc_samr_Close(h, mem_ctx, &user_handle, &result);
801 talloc_free(mem_ctx);
803 return status;
806 /*************************************************************************
807 _netr_ServerReqChallenge
808 *************************************************************************/
810 NTSTATUS _netr_ServerReqChallenge(struct pipes_struct *p,
811 struct netr_ServerReqChallenge *r)
813 struct netlogon_server_pipe_state *pipe_state =
814 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
816 if (pipe_state) {
817 DEBUG(10,("_netr_ServerReqChallenge: new challenge requested. Clearing old state.\n"));
818 talloc_free(pipe_state);
819 p->private_data = NULL;
822 pipe_state = talloc(p, struct netlogon_server_pipe_state);
823 NT_STATUS_HAVE_NO_MEMORY(pipe_state);
825 pipe_state->client_challenge = *r->in.credentials;
827 generate_random_buffer(pipe_state->server_challenge.data,
828 sizeof(pipe_state->server_challenge.data));
830 *r->out.return_credentials = pipe_state->server_challenge;
832 p->private_data = pipe_state;
834 return NT_STATUS_OK;
837 /*************************************************************************
838 _netr_ServerAuthenticate
839 Create the initial credentials.
840 *************************************************************************/
842 NTSTATUS _netr_ServerAuthenticate(struct pipes_struct *p,
843 struct netr_ServerAuthenticate *r)
845 struct netr_ServerAuthenticate3 a;
846 uint32_t negotiate_flags = 0;
847 uint32_t rid;
849 a.in.server_name = r->in.server_name;
850 a.in.account_name = r->in.account_name;
851 a.in.secure_channel_type = r->in.secure_channel_type;
852 a.in.computer_name = r->in.computer_name;
853 a.in.credentials = r->in.credentials;
854 a.in.negotiate_flags = &negotiate_flags;
856 a.out.return_credentials = r->out.return_credentials;
857 a.out.rid = &rid;
858 a.out.negotiate_flags = &negotiate_flags;
860 return _netr_ServerAuthenticate3(p, &a);
864 /*************************************************************************
865 _netr_ServerAuthenticate3
866 *************************************************************************/
868 NTSTATUS _netr_ServerAuthenticate3(struct pipes_struct *p,
869 struct netr_ServerAuthenticate3 *r)
871 NTSTATUS status;
872 uint32_t srv_flgs;
873 /* r->in.negotiate_flags is an aliased pointer to r->out.negotiate_flags,
874 * so use a copy to avoid destroying the client values. */
875 uint32_t in_neg_flags = *r->in.negotiate_flags;
876 const char *fn;
877 struct dom_sid sid;
878 struct samr_Password mach_pwd;
879 struct netlogon_creds_CredentialState *creds;
880 struct netlogon_server_pipe_state *pipe_state =
881 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
883 /* According to Microsoft (see bugid #6099)
884 * Windows 7 looks at the negotiate_flags
885 * returned in this structure *even if the
886 * call fails with access denied* ! So in order
887 * to allow Win7 to connect to a Samba NT style
888 * PDC we set the flags before we know if it's
889 * an error or not.
892 /* 0x000001ff */
893 srv_flgs = NETLOGON_NEG_ACCOUNT_LOCKOUT |
894 NETLOGON_NEG_PERSISTENT_SAMREPL |
895 NETLOGON_NEG_ARCFOUR |
896 NETLOGON_NEG_PROMOTION_COUNT |
897 NETLOGON_NEG_CHANGELOG_BDC |
898 NETLOGON_NEG_FULL_SYNC_REPL |
899 NETLOGON_NEG_MULTIPLE_SIDS |
900 NETLOGON_NEG_REDO |
901 NETLOGON_NEG_PASSWORD_CHANGE_REFUSAL |
902 NETLOGON_NEG_PASSWORD_SET2;
904 /* Ensure we support strong (128-bit) keys. */
905 if (in_neg_flags & NETLOGON_NEG_STRONG_KEYS) {
906 srv_flgs |= NETLOGON_NEG_STRONG_KEYS;
909 if (lp_server_schannel() != false) {
910 srv_flgs |= NETLOGON_NEG_SCHANNEL;
913 switch (p->opnum) {
914 case NDR_NETR_SERVERAUTHENTICATE:
915 fn = "_netr_ServerAuthenticate";
916 break;
917 case NDR_NETR_SERVERAUTHENTICATE2:
918 fn = "_netr_ServerAuthenticate2";
919 break;
920 case NDR_NETR_SERVERAUTHENTICATE3:
921 fn = "_netr_ServerAuthenticate3";
922 break;
923 default:
924 return NT_STATUS_INTERNAL_ERROR;
927 /* We use this as the key to store the creds: */
928 /* r->in.computer_name */
930 if (!pipe_state) {
931 DEBUG(0,("%s: no challenge sent to client %s\n", fn,
932 r->in.computer_name));
933 status = NT_STATUS_ACCESS_DENIED;
934 goto out;
937 if ( (lp_server_schannel() == true) &&
938 ((in_neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
940 /* schannel must be used, but client did not offer it. */
941 DEBUG(0,("%s: schannel required but client failed "
942 "to offer it. Client was %s\n",
943 fn, r->in.account_name));
944 status = NT_STATUS_ACCESS_DENIED;
945 goto out;
948 status = get_md4pw(&mach_pwd,
949 r->in.account_name,
950 r->in.secure_channel_type,
951 &sid, p->msg_ctx);
952 if (!NT_STATUS_IS_OK(status)) {
953 DEBUG(0,("%s: failed to get machine password for "
954 "account %s: %s\n",
955 fn, r->in.account_name, nt_errstr(status) ));
956 /* always return NT_STATUS_ACCESS_DENIED */
957 status = NT_STATUS_ACCESS_DENIED;
958 goto out;
961 /* From the client / server challenges and md4 password, generate sess key */
962 /* Check client credentials are valid. */
963 creds = netlogon_creds_server_init(p->mem_ctx,
964 r->in.account_name,
965 r->in.computer_name,
966 r->in.secure_channel_type,
967 &pipe_state->client_challenge,
968 &pipe_state->server_challenge,
969 &mach_pwd,
970 r->in.credentials,
971 r->out.return_credentials,
972 *r->in.negotiate_flags);
973 if (!creds) {
974 DEBUG(0,("%s: netlogon_creds_server_check failed. Rejecting auth "
975 "request from client %s machine account %s\n",
976 fn, r->in.computer_name,
977 r->in.account_name));
978 status = NT_STATUS_ACCESS_DENIED;
979 goto out;
982 creds->sid = dom_sid_dup(creds, &sid);
983 if (!creds->sid) {
984 status = NT_STATUS_NO_MEMORY;
985 goto out;
988 /* Store off the state so we can continue after client disconnect. */
989 become_root();
990 status = schannel_save_creds_state(p->mem_ctx, lp_private_dir(), creds);
991 unbecome_root();
993 if (!NT_STATUS_IS_OK(status)) {
994 goto out;
997 sid_peek_rid(&sid, r->out.rid);
999 status = NT_STATUS_OK;
1001 out:
1003 *r->out.negotiate_flags = srv_flgs;
1004 return status;
1007 /*************************************************************************
1008 _netr_ServerAuthenticate2
1009 *************************************************************************/
1011 NTSTATUS _netr_ServerAuthenticate2(struct pipes_struct *p,
1012 struct netr_ServerAuthenticate2 *r)
1014 struct netr_ServerAuthenticate3 a;
1015 uint32_t rid;
1017 a.in.server_name = r->in.server_name;
1018 a.in.account_name = r->in.account_name;
1019 a.in.secure_channel_type = r->in.secure_channel_type;
1020 a.in.computer_name = r->in.computer_name;
1021 a.in.credentials = r->in.credentials;
1022 a.in.negotiate_flags = r->in.negotiate_flags;
1024 a.out.return_credentials = r->out.return_credentials;
1025 a.out.rid = &rid;
1026 a.out.negotiate_flags = r->out.negotiate_flags;
1028 return _netr_ServerAuthenticate3(p, &a);
1031 /*************************************************************************
1032 * If schannel is required for this call test that it actually is available.
1033 *************************************************************************/
1034 static NTSTATUS schannel_check_required(struct pipe_auth_data *auth_info,
1035 const char *computer_name,
1036 bool integrity, bool privacy)
1038 if (auth_info && auth_info->auth_type == DCERPC_AUTH_TYPE_SCHANNEL) {
1039 if (!privacy && !integrity) {
1040 return NT_STATUS_OK;
1043 if ((!privacy && integrity) &&
1044 auth_info->auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
1045 return NT_STATUS_OK;
1048 if ((privacy || integrity) &&
1049 auth_info->auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1050 return NT_STATUS_OK;
1054 /* test didn't pass */
1055 DEBUG(0, ("schannel_check_required: [%s] is not using schannel\n",
1056 computer_name));
1058 return NT_STATUS_ACCESS_DENIED;
1061 /*************************************************************************
1062 *************************************************************************/
1064 static NTSTATUS netr_creds_server_step_check(struct pipes_struct *p,
1065 TALLOC_CTX *mem_ctx,
1066 const char *computer_name,
1067 struct netr_Authenticator *received_authenticator,
1068 struct netr_Authenticator *return_authenticator,
1069 struct netlogon_creds_CredentialState **creds_out)
1071 NTSTATUS status;
1072 bool schannel_global_required = (lp_server_schannel() == true) ? true:false;
1074 if (schannel_global_required) {
1075 status = schannel_check_required(&p->auth,
1076 computer_name,
1077 false, false);
1078 if (!NT_STATUS_IS_OK(status)) {
1079 return status;
1083 status = schannel_check_creds_state(mem_ctx, lp_private_dir(),
1084 computer_name, received_authenticator,
1085 return_authenticator, creds_out);
1087 return status;
1090 /*************************************************************************
1091 *************************************************************************/
1093 static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
1094 struct auth_serversupplied_info *session_info,
1095 struct messaging_context *msg_ctx,
1096 const char *account_name,
1097 struct samr_Password *nt_hash)
1099 NTSTATUS status;
1100 NTSTATUS result = NT_STATUS_OK;
1101 struct dcerpc_binding_handle *h = NULL;
1102 static struct client_address client_id;
1103 struct policy_handle user_handle;
1104 uint32_t acct_ctrl;
1105 union samr_UserInfo *info;
1106 struct samr_UserInfo18 info18;
1107 DATA_BLOB in,out;
1109 ZERO_STRUCT(user_handle);
1111 strlcpy(client_id.addr, "127.0.0.1", sizeof(client_id.addr));
1112 client_id.name = "127.0.0.1";
1114 status = rpcint_binding_handle(mem_ctx,
1115 &ndr_table_samr,
1116 &client_id,
1117 session_info,
1118 msg_ctx,
1119 &h);
1120 if (!NT_STATUS_IS_OK(status)) {
1121 goto out;
1124 status = samr_find_machine_account(mem_ctx,
1126 account_name,
1127 SEC_FLAG_MAXIMUM_ALLOWED,
1128 NULL,
1129 NULL,
1130 &user_handle);
1131 if (!NT_STATUS_IS_OK(status)) {
1132 goto out;
1135 status = dcerpc_samr_QueryUserInfo2(h,
1136 mem_ctx,
1137 &user_handle,
1138 UserControlInformation,
1139 &info,
1140 &result);
1141 if (!NT_STATUS_IS_OK(status)) {
1142 goto out;
1144 if (!NT_STATUS_IS_OK(result)) {
1145 status = result;
1146 goto out;
1149 acct_ctrl = info->info16.acct_flags;
1151 if (!(acct_ctrl & ACB_WSTRUST ||
1152 acct_ctrl & ACB_SVRTRUST ||
1153 acct_ctrl & ACB_DOMTRUST)) {
1154 status = NT_STATUS_NO_SUCH_USER;
1155 goto out;
1158 if (acct_ctrl & ACB_DISABLED) {
1159 status = NT_STATUS_ACCOUNT_DISABLED;
1160 goto out;
1163 ZERO_STRUCT(info18);
1165 in = data_blob_const(nt_hash->hash, 16);
1166 out = data_blob_talloc_zero(mem_ctx, 16);
1167 sess_crypt_blob(&out, &in, &session_info->user_session_key, true);
1168 memcpy(info18.nt_pwd.hash, out.data, out.length);
1170 info18.nt_pwd_active = true;
1172 info->info18 = info18;
1174 status = dcerpc_samr_SetUserInfo2(h,
1175 mem_ctx,
1176 &user_handle,
1177 UserInternal1Information,
1178 info,
1179 &result);
1180 if (!NT_STATUS_IS_OK(status)) {
1181 goto out;
1183 if (!NT_STATUS_IS_OK(result)) {
1184 status = result;
1185 goto out;
1188 out:
1189 if (h && is_valid_policy_hnd(&user_handle)) {
1190 dcerpc_samr_Close(h, mem_ctx, &user_handle, &result);
1193 return status;
1196 /*************************************************************************
1197 _netr_ServerPasswordSet
1198 *************************************************************************/
1200 NTSTATUS _netr_ServerPasswordSet(struct pipes_struct *p,
1201 struct netr_ServerPasswordSet *r)
1203 NTSTATUS status = NT_STATUS_OK;
1204 int i;
1205 struct netlogon_creds_CredentialState *creds;
1207 DEBUG(5,("_netr_ServerPasswordSet: %d\n", __LINE__));
1209 become_root();
1210 status = netr_creds_server_step_check(p, p->mem_ctx,
1211 r->in.computer_name,
1212 r->in.credential,
1213 r->out.return_authenticator,
1214 &creds);
1215 unbecome_root();
1217 if (!NT_STATUS_IS_OK(status)) {
1218 DEBUG(2,("_netr_ServerPasswordSet: netlogon_creds_server_step failed. Rejecting auth "
1219 "request from client %s machine account %s\n",
1220 r->in.computer_name, creds->computer_name));
1221 TALLOC_FREE(creds);
1222 return status;
1225 DEBUG(3,("_netr_ServerPasswordSet: Server Password Set by remote machine:[%s] on account [%s]\n",
1226 r->in.computer_name, creds->computer_name));
1228 netlogon_creds_des_decrypt(creds, r->in.new_password);
1230 DEBUG(100,("_netr_ServerPasswordSet: new given value was :\n"));
1231 for(i = 0; i < sizeof(r->in.new_password->hash); i++)
1232 DEBUG(100,("%02X ", r->in.new_password->hash[i]));
1233 DEBUG(100,("\n"));
1235 status = netr_set_machine_account_password(p->mem_ctx,
1236 p->session_info,
1237 p->msg_ctx,
1238 creds->account_name,
1239 r->in.new_password);
1240 return status;
1243 /****************************************************************
1244 _netr_ServerPasswordSet2
1245 ****************************************************************/
1247 NTSTATUS _netr_ServerPasswordSet2(struct pipes_struct *p,
1248 struct netr_ServerPasswordSet2 *r)
1250 NTSTATUS status;
1251 struct netlogon_creds_CredentialState *creds;
1252 DATA_BLOB plaintext;
1253 struct samr_CryptPassword password_buf;
1254 struct samr_Password nt_hash;
1256 become_root();
1257 status = netr_creds_server_step_check(p, p->mem_ctx,
1258 r->in.computer_name,
1259 r->in.credential,
1260 r->out.return_authenticator,
1261 &creds);
1262 unbecome_root();
1264 if (!NT_STATUS_IS_OK(status)) {
1265 DEBUG(2,("_netr_ServerPasswordSet2: netlogon_creds_server_step "
1266 "failed. Rejecting auth request from client %s machine account %s\n",
1267 r->in.computer_name, creds->computer_name));
1268 TALLOC_FREE(creds);
1269 return status;
1272 memcpy(password_buf.data, r->in.new_password->data, 512);
1273 SIVAL(password_buf.data, 512, r->in.new_password->length);
1274 netlogon_creds_arcfour_crypt(creds, password_buf.data, 516);
1276 if (!extract_pw_from_buffer(p->mem_ctx, password_buf.data, &plaintext)) {
1277 return NT_STATUS_WRONG_PASSWORD;
1280 mdfour(nt_hash.hash, plaintext.data, plaintext.length);
1282 status = netr_set_machine_account_password(p->mem_ctx,
1283 p->session_info,
1284 p->msg_ctx,
1285 creds->account_name,
1286 &nt_hash);
1287 return status;
1290 /*************************************************************************
1291 _netr_LogonSamLogoff
1292 *************************************************************************/
1294 NTSTATUS _netr_LogonSamLogoff(struct pipes_struct *p,
1295 struct netr_LogonSamLogoff *r)
1297 NTSTATUS status;
1298 struct netlogon_creds_CredentialState *creds;
1300 become_root();
1301 status = netr_creds_server_step_check(p, p->mem_ctx,
1302 r->in.computer_name,
1303 r->in.credential,
1304 r->out.return_authenticator,
1305 &creds);
1306 unbecome_root();
1308 return status;
1311 static NTSTATUS _netr_LogonSamLogon_check(const struct netr_LogonSamLogonEx *r)
1313 switch (r->in.logon_level) {
1314 case NetlogonInteractiveInformation:
1315 case NetlogonServiceInformation:
1316 case NetlogonInteractiveTransitiveInformation:
1317 case NetlogonServiceTransitiveInformation:
1318 if (r->in.logon->password == NULL) {
1319 return NT_STATUS_INVALID_PARAMETER;
1322 switch (r->in.validation_level) {
1323 case NetlogonValidationSamInfo: /* 2 */
1324 case NetlogonValidationSamInfo2: /* 3 */
1325 break;
1326 case NetlogonValidationSamInfo4: /* 6 */
1327 if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
1328 DEBUG(10,("Not adding validation info level 6 "
1329 "without ADS passdb backend\n"));
1330 return NT_STATUS_INVALID_INFO_CLASS;
1332 break;
1333 default:
1334 return NT_STATUS_INVALID_INFO_CLASS;
1337 break;
1338 case NetlogonNetworkInformation:
1339 case NetlogonNetworkTransitiveInformation:
1340 if (r->in.logon->network == NULL) {
1341 return NT_STATUS_INVALID_PARAMETER;
1344 switch (r->in.validation_level) {
1345 case NetlogonValidationSamInfo: /* 2 */
1346 case NetlogonValidationSamInfo2: /* 3 */
1347 break;
1348 case NetlogonValidationSamInfo4: /* 6 */
1349 if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
1350 DEBUG(10,("Not adding validation info level 6 "
1351 "without ADS passdb backend\n"));
1352 return NT_STATUS_INVALID_INFO_CLASS;
1354 break;
1355 default:
1356 return NT_STATUS_INVALID_INFO_CLASS;
1359 break;
1361 case NetlogonGenericInformation:
1362 if (r->in.logon->generic == NULL) {
1363 return NT_STATUS_INVALID_PARAMETER;
1366 /* we don't support this here */
1367 return NT_STATUS_INVALID_PARAMETER;
1368 #if 0
1369 switch (r->in.validation_level) {
1370 /* TODO: case NetlogonValidationGenericInfo: 4 */
1371 case NetlogonValidationGenericInfo2: /* 5 */
1372 break;
1373 default:
1374 return NT_STATUS_INVALID_INFO_CLASS;
1377 break;
1378 #endif
1379 default:
1380 return NT_STATUS_INVALID_PARAMETER;
1383 return NT_STATUS_OK;
1386 /*************************************************************************
1387 _netr_LogonSamLogon_base
1388 *************************************************************************/
1390 static NTSTATUS _netr_LogonSamLogon_base(struct pipes_struct *p,
1391 struct netr_LogonSamLogonEx *r,
1392 struct netlogon_creds_CredentialState *creds)
1394 NTSTATUS status = NT_STATUS_OK;
1395 union netr_LogonLevel *logon = r->in.logon;
1396 const char *nt_username, *nt_domain, *nt_workstation;
1397 struct auth_usersupplied_info *user_info = NULL;
1398 struct auth_serversupplied_info *server_info = NULL;
1399 struct auth_context *auth_context = NULL;
1400 uint8_t pipe_session_key[16];
1401 bool process_creds = true;
1402 const char *fn;
1404 switch (p->opnum) {
1405 case NDR_NETR_LOGONSAMLOGON:
1406 process_creds = true;
1407 fn = "_netr_LogonSamLogon";
1408 break;
1409 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
1410 process_creds = true;
1411 fn = "_netr_LogonSamLogonWithFlags";
1412 break;
1413 case NDR_NETR_LOGONSAMLOGONEX:
1414 process_creds = false;
1415 fn = "_netr_LogonSamLogonEx";
1416 break;
1417 default:
1418 return NT_STATUS_INTERNAL_ERROR;
1421 *r->out.authoritative = true; /* authoritative response */
1423 switch (r->in.validation_level) {
1424 case 2:
1425 r->out.validation->sam2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo2);
1426 if (!r->out.validation->sam2) {
1427 return NT_STATUS_NO_MEMORY;
1429 break;
1430 case 3:
1431 r->out.validation->sam3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo3);
1432 if (!r->out.validation->sam3) {
1433 return NT_STATUS_NO_MEMORY;
1435 break;
1436 case 6:
1437 r->out.validation->sam6 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo6);
1438 if (!r->out.validation->sam6) {
1439 return NT_STATUS_NO_MEMORY;
1441 break;
1442 default:
1443 DEBUG(0,("%s: bad validation_level value %d.\n",
1444 fn, (int)r->in.validation_level));
1445 return NT_STATUS_INVALID_INFO_CLASS;
1448 switch (r->in.logon_level) {
1449 case NetlogonInteractiveInformation:
1450 case NetlogonServiceInformation:
1451 case NetlogonInteractiveTransitiveInformation:
1452 case NetlogonServiceTransitiveInformation:
1453 nt_username = logon->password->identity_info.account_name.string ?
1454 logon->password->identity_info.account_name.string : "";
1455 nt_domain = logon->password->identity_info.domain_name.string ?
1456 logon->password->identity_info.domain_name.string : "";
1457 nt_workstation = logon->password->identity_info.workstation.string ?
1458 logon->password->identity_info.workstation.string : "";
1460 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
1461 break;
1462 case NetlogonNetworkInformation:
1463 case NetlogonNetworkTransitiveInformation:
1464 nt_username = logon->network->identity_info.account_name.string ?
1465 logon->network->identity_info.account_name.string : "";
1466 nt_domain = logon->network->identity_info.domain_name.string ?
1467 logon->network->identity_info.domain_name.string : "";
1468 nt_workstation = logon->network->identity_info.workstation.string ?
1469 logon->network->identity_info.workstation.string : "";
1471 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
1472 break;
1473 default:
1474 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1475 return NT_STATUS_INVALID_INFO_CLASS;
1476 } /* end switch */
1478 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
1479 fstrcpy(current_user_info.smb_name, nt_username);
1480 sub_set_smb_name(nt_username);
1482 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
1483 r->in.validation_level, nt_username));
1485 status = NT_STATUS_OK;
1487 switch (r->in.logon_level) {
1488 case NetlogonNetworkInformation:
1489 case NetlogonNetworkTransitiveInformation:
1491 const char *wksname = nt_workstation;
1493 status = make_auth_context_fixed(talloc_tos(), &auth_context,
1494 logon->network->challenge);
1495 if (!NT_STATUS_IS_OK(status)) {
1496 return status;
1499 /* For a network logon, the workstation name comes in with two
1500 * backslashes in the front. Strip them if they are there. */
1502 if (*wksname == '\\') wksname++;
1503 if (*wksname == '\\') wksname++;
1505 /* Standard challenge/response authentication */
1506 if (!make_user_info_netlogon_network(&user_info,
1507 nt_username, nt_domain,
1508 wksname,
1509 logon->network->identity_info.parameter_control,
1510 logon->network->lm.data,
1511 logon->network->lm.length,
1512 logon->network->nt.data,
1513 logon->network->nt.length)) {
1514 status = NT_STATUS_NO_MEMORY;
1516 break;
1518 case NetlogonInteractiveInformation:
1519 case NetlogonServiceInformation:
1520 case NetlogonInteractiveTransitiveInformation:
1521 case NetlogonServiceTransitiveInformation:
1523 /* 'Interactive' authentication, supplies the password in its
1524 MD4 form, encrypted with the session key. We will convert
1525 this to challenge/response for the auth subsystem to chew
1526 on */
1528 uint8_t chal[8];
1530 status = make_auth_context_subsystem(talloc_tos(),
1531 &auth_context);
1532 if (!NT_STATUS_IS_OK(status)) {
1533 return status;
1536 auth_context->get_ntlm_challenge(auth_context, chal);
1538 if (!make_user_info_netlogon_interactive(&user_info,
1539 nt_username, nt_domain,
1540 nt_workstation,
1541 logon->password->identity_info.parameter_control,
1542 chal,
1543 logon->password->lmpassword.hash,
1544 logon->password->ntpassword.hash,
1545 creds->session_key)) {
1546 status = NT_STATUS_NO_MEMORY;
1548 break;
1550 default:
1551 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1552 return NT_STATUS_INVALID_INFO_CLASS;
1553 } /* end switch */
1555 if ( NT_STATUS_IS_OK(status) ) {
1556 status = auth_context->check_ntlm_password(auth_context,
1557 user_info, &server_info);
1560 TALLOC_FREE(auth_context);
1561 free_user_info(&user_info);
1563 DEBUG(5,("%s: check_password returned status %s\n",
1564 fn, nt_errstr(status)));
1566 /* Check account and password */
1568 if (!NT_STATUS_IS_OK(status)) {
1569 /* If we don't know what this domain is, we need to
1570 indicate that we are not authoritative. This
1571 allows the client to decide if it needs to try
1572 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
1573 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1574 && !strequal(nt_domain, get_global_sam_name())
1575 && !is_trusted_domain(nt_domain) )
1576 *r->out.authoritative = false; /* We are not authoritative */
1578 TALLOC_FREE(server_info);
1579 return status;
1582 if (server_info->guest) {
1583 /* We don't like guest domain logons... */
1584 DEBUG(5,("%s: Attempted domain logon as GUEST "
1585 "denied.\n", fn));
1586 TALLOC_FREE(server_info);
1587 return NT_STATUS_LOGON_FAILURE;
1590 /* This is the point at which, if the login was successful, that
1591 the SAM Local Security Authority should record that the user is
1592 logged in to the domain. */
1594 if (process_creds) {
1595 /* Get the pipe session key from the creds. */
1596 memcpy(pipe_session_key, creds->session_key, 16);
1597 } else {
1598 struct schannel_state *schannel_auth;
1599 /* Get the pipe session key from the schannel. */
1600 if ((p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL)
1601 || (p->auth.auth_ctx == NULL)) {
1602 return NT_STATUS_INVALID_HANDLE;
1605 schannel_auth = talloc_get_type_abort(p->auth.auth_ctx,
1606 struct schannel_state);
1607 memcpy(pipe_session_key, schannel_auth->creds->session_key, 16);
1610 switch (r->in.validation_level) {
1611 case 2:
1612 status = serverinfo_to_SamInfo2(server_info, pipe_session_key, 16,
1613 r->out.validation->sam2);
1614 break;
1615 case 3:
1616 status = serverinfo_to_SamInfo3(server_info, pipe_session_key, 16,
1617 r->out.validation->sam3);
1618 break;
1619 case 6:
1620 status = serverinfo_to_SamInfo6(server_info, pipe_session_key, 16,
1621 r->out.validation->sam6);
1622 break;
1625 TALLOC_FREE(server_info);
1627 return status;
1630 /****************************************************************
1631 _netr_LogonSamLogonWithFlags
1632 ****************************************************************/
1634 NTSTATUS _netr_LogonSamLogonWithFlags(struct pipes_struct *p,
1635 struct netr_LogonSamLogonWithFlags *r)
1637 NTSTATUS status;
1638 struct netlogon_creds_CredentialState *creds;
1639 struct netr_LogonSamLogonEx r2;
1640 struct netr_Authenticator return_authenticator;
1642 *r->out.authoritative = true;
1644 r2.in.server_name = r->in.server_name;
1645 r2.in.computer_name = r->in.computer_name;
1646 r2.in.logon_level = r->in.logon_level;
1647 r2.in.logon = r->in.logon;
1648 r2.in.validation_level = r->in.validation_level;
1649 r2.in.flags = r->in.flags;
1650 r2.out.validation = r->out.validation;
1651 r2.out.authoritative = r->out.authoritative;
1652 r2.out.flags = r->out.flags;
1654 status = _netr_LogonSamLogon_check(&r2);
1655 if (!NT_STATUS_IS_OK(status)) {
1656 return status;
1659 become_root();
1660 status = netr_creds_server_step_check(p, p->mem_ctx,
1661 r->in.computer_name,
1662 r->in.credential,
1663 &return_authenticator,
1664 &creds);
1665 unbecome_root();
1666 if (!NT_STATUS_IS_OK(status)) {
1667 return status;
1670 status = _netr_LogonSamLogon_base(p, &r2, creds);
1672 *r->out.return_authenticator = return_authenticator;
1674 return status;
1677 /*************************************************************************
1678 _netr_LogonSamLogon
1679 *************************************************************************/
1681 NTSTATUS _netr_LogonSamLogon(struct pipes_struct *p,
1682 struct netr_LogonSamLogon *r)
1684 NTSTATUS status;
1685 struct netr_LogonSamLogonWithFlags r2;
1686 uint32_t flags = 0;
1688 r2.in.server_name = r->in.server_name;
1689 r2.in.computer_name = r->in.computer_name;
1690 r2.in.credential = r->in.credential;
1691 r2.in.logon_level = r->in.logon_level;
1692 r2.in.logon = r->in.logon;
1693 r2.in.validation_level = r->in.validation_level;
1694 r2.in.return_authenticator = r->in.return_authenticator;
1695 r2.in.flags = &flags;
1696 r2.out.validation = r->out.validation;
1697 r2.out.authoritative = r->out.authoritative;
1698 r2.out.flags = &flags;
1699 r2.out.return_authenticator = r->out.return_authenticator;
1701 status = _netr_LogonSamLogonWithFlags(p, &r2);
1703 return status;
1706 /*************************************************************************
1707 _netr_LogonSamLogonEx
1708 - no credential chaining. Map into net sam logon.
1709 *************************************************************************/
1711 NTSTATUS _netr_LogonSamLogonEx(struct pipes_struct *p,
1712 struct netr_LogonSamLogonEx *r)
1714 NTSTATUS status;
1715 struct netlogon_creds_CredentialState *creds = NULL;
1717 *r->out.authoritative = true;
1719 status = _netr_LogonSamLogon_check(r);
1720 if (!NT_STATUS_IS_OK(status)) {
1721 return status;
1724 /* Only allow this if the pipe is protected. */
1725 if (p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
1726 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1727 get_remote_machine_name() ));
1728 return NT_STATUS_INVALID_PARAMETER;
1731 become_root();
1732 status = schannel_get_creds_state(p->mem_ctx, lp_private_dir(),
1733 r->in.computer_name, &creds);
1734 unbecome_root();
1735 if (!NT_STATUS_IS_OK(status)) {
1736 return status;
1739 status = _netr_LogonSamLogon_base(p, r, creds);
1740 TALLOC_FREE(creds);
1742 return status;
1745 /*************************************************************************
1746 _ds_enum_dom_trusts
1747 *************************************************************************/
1748 #if 0 /* JERRY -- not correct */
1749 NTSTATUS _ds_enum_dom_trusts(struct pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1750 DS_R_ENUM_DOM_TRUSTS *r_u)
1752 NTSTATUS status = NT_STATUS_OK;
1754 /* TODO: According to MSDN, the can only be executed against a
1755 DC or domain member running Windows 2000 or later. Need
1756 to test against a standalone 2k server and see what it
1757 does. A windows 2000 DC includes its own domain in the
1758 list. --jerry */
1760 return status;
1762 #endif /* JERRY */
1765 /****************************************************************
1766 ****************************************************************/
1768 WERROR _netr_LogonUasLogon(struct pipes_struct *p,
1769 struct netr_LogonUasLogon *r)
1771 p->rng_fault_state = true;
1772 return WERR_NOT_SUPPORTED;
1775 /****************************************************************
1776 ****************************************************************/
1778 WERROR _netr_LogonUasLogoff(struct pipes_struct *p,
1779 struct netr_LogonUasLogoff *r)
1781 p->rng_fault_state = true;
1782 return WERR_NOT_SUPPORTED;
1785 /****************************************************************
1786 ****************************************************************/
1788 NTSTATUS _netr_DatabaseDeltas(struct pipes_struct *p,
1789 struct netr_DatabaseDeltas *r)
1791 p->rng_fault_state = true;
1792 return NT_STATUS_NOT_IMPLEMENTED;
1795 /****************************************************************
1796 ****************************************************************/
1798 NTSTATUS _netr_DatabaseSync(struct pipes_struct *p,
1799 struct netr_DatabaseSync *r)
1801 p->rng_fault_state = true;
1802 return NT_STATUS_NOT_IMPLEMENTED;
1805 /****************************************************************
1806 ****************************************************************/
1808 NTSTATUS _netr_AccountDeltas(struct pipes_struct *p,
1809 struct netr_AccountDeltas *r)
1811 p->rng_fault_state = true;
1812 return NT_STATUS_NOT_IMPLEMENTED;
1815 /****************************************************************
1816 ****************************************************************/
1818 NTSTATUS _netr_AccountSync(struct pipes_struct *p,
1819 struct netr_AccountSync *r)
1821 p->rng_fault_state = true;
1822 return NT_STATUS_NOT_IMPLEMENTED;
1825 /****************************************************************
1826 ****************************************************************/
1828 static bool wb_getdcname(TALLOC_CTX *mem_ctx,
1829 const char *domain,
1830 const char **dcname,
1831 uint32_t flags,
1832 WERROR *werr)
1834 wbcErr result;
1835 struct wbcDomainControllerInfo *dc_info = NULL;
1837 result = wbcLookupDomainController(domain,
1838 flags,
1839 &dc_info);
1840 switch (result) {
1841 case WBC_ERR_SUCCESS:
1842 break;
1843 case WBC_ERR_WINBIND_NOT_AVAILABLE:
1844 return false;
1845 case WBC_ERR_DOMAIN_NOT_FOUND:
1846 *werr = WERR_NO_SUCH_DOMAIN;
1847 return true;
1848 default:
1849 *werr = WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1850 return true;
1853 *dcname = talloc_strdup(mem_ctx, dc_info->dc_name);
1854 wbcFreeMemory(dc_info);
1855 if (!*dcname) {
1856 *werr = WERR_NOMEM;
1857 return false;
1860 *werr = WERR_OK;
1862 return true;
1865 /****************************************************************
1866 _netr_GetDcName
1867 ****************************************************************/
1869 WERROR _netr_GetDcName(struct pipes_struct *p,
1870 struct netr_GetDcName *r)
1872 NTSTATUS status;
1873 WERROR werr;
1874 uint32_t flags;
1875 struct netr_DsRGetDCNameInfo *info;
1876 bool ret;
1878 ret = wb_getdcname(p->mem_ctx,
1879 r->in.domainname,
1880 r->out.dcname,
1881 WBC_LOOKUP_DC_IS_FLAT_NAME |
1882 WBC_LOOKUP_DC_RETURN_FLAT_NAME |
1883 WBC_LOOKUP_DC_PDC_REQUIRED,
1884 &werr);
1885 if (ret == true) {
1886 return werr;
1889 flags = DS_PDC_REQUIRED | DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1891 status = dsgetdcname(p->mem_ctx,
1892 p->msg_ctx,
1893 r->in.domainname,
1894 NULL,
1895 NULL,
1896 flags,
1897 &info);
1898 if (!NT_STATUS_IS_OK(status)) {
1899 return ntstatus_to_werror(status);
1902 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1903 talloc_free(info);
1904 if (!*r->out.dcname) {
1905 return WERR_NOMEM;
1908 return WERR_OK;
1911 /****************************************************************
1912 _netr_GetAnyDCName
1913 ****************************************************************/
1915 WERROR _netr_GetAnyDCName(struct pipes_struct *p,
1916 struct netr_GetAnyDCName *r)
1918 NTSTATUS status;
1919 WERROR werr;
1920 uint32_t flags;
1921 struct netr_DsRGetDCNameInfo *info;
1922 bool ret;
1924 ret = wb_getdcname(p->mem_ctx,
1925 r->in.domainname,
1926 r->out.dcname,
1927 WBC_LOOKUP_DC_IS_FLAT_NAME |
1928 WBC_LOOKUP_DC_RETURN_FLAT_NAME,
1929 &werr);
1930 if (ret == true) {
1931 return werr;
1934 flags = DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1936 status = dsgetdcname(p->mem_ctx,
1937 p->msg_ctx,
1938 r->in.domainname,
1939 NULL,
1940 NULL,
1941 flags,
1942 &info);
1943 if (!NT_STATUS_IS_OK(status)) {
1944 return ntstatus_to_werror(status);
1947 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1948 talloc_free(info);
1949 if (!*r->out.dcname) {
1950 return WERR_NOMEM;
1953 return WERR_OK;
1956 /****************************************************************
1957 ****************************************************************/
1959 NTSTATUS _netr_DatabaseSync2(struct pipes_struct *p,
1960 struct netr_DatabaseSync2 *r)
1962 p->rng_fault_state = true;
1963 return NT_STATUS_NOT_IMPLEMENTED;
1966 /****************************************************************
1967 ****************************************************************/
1969 NTSTATUS _netr_DatabaseRedo(struct pipes_struct *p,
1970 struct netr_DatabaseRedo *r)
1972 p->rng_fault_state = true;
1973 return NT_STATUS_NOT_IMPLEMENTED;
1976 /****************************************************************
1977 ****************************************************************/
1979 WERROR _netr_DsRGetDCName(struct pipes_struct *p,
1980 struct netr_DsRGetDCName *r)
1982 p->rng_fault_state = true;
1983 return WERR_NOT_SUPPORTED;
1986 /****************************************************************
1987 ****************************************************************/
1989 NTSTATUS _netr_LogonGetCapabilities(struct pipes_struct *p,
1990 struct netr_LogonGetCapabilities *r)
1992 return NT_STATUS_NOT_IMPLEMENTED;
1995 /****************************************************************
1996 ****************************************************************/
1998 WERROR _netr_NETRLOGONSETSERVICEBITS(struct pipes_struct *p,
1999 struct netr_NETRLOGONSETSERVICEBITS *r)
2001 p->rng_fault_state = true;
2002 return WERR_NOT_SUPPORTED;
2005 /****************************************************************
2006 ****************************************************************/
2008 WERROR _netr_LogonGetTrustRid(struct pipes_struct *p,
2009 struct netr_LogonGetTrustRid *r)
2011 p->rng_fault_state = true;
2012 return WERR_NOT_SUPPORTED;
2015 /****************************************************************
2016 ****************************************************************/
2018 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(struct pipes_struct *p,
2019 struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
2021 p->rng_fault_state = true;
2022 return WERR_NOT_SUPPORTED;
2025 /****************************************************************
2026 ****************************************************************/
2028 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(struct pipes_struct *p,
2029 struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
2031 p->rng_fault_state = true;
2032 return WERR_NOT_SUPPORTED;
2035 /****************************************************************
2036 ****************************************************************/
2038 WERROR _netr_DsRGetDCNameEx(struct pipes_struct *p,
2039 struct netr_DsRGetDCNameEx *r)
2041 p->rng_fault_state = true;
2042 return WERR_NOT_SUPPORTED;
2045 /****************************************************************
2046 ****************************************************************/
2048 WERROR _netr_DsRGetSiteName(struct pipes_struct *p,
2049 struct netr_DsRGetSiteName *r)
2051 p->rng_fault_state = true;
2052 return WERR_NOT_SUPPORTED;
2055 /****************************************************************
2056 ****************************************************************/
2058 NTSTATUS _netr_LogonGetDomainInfo(struct pipes_struct *p,
2059 struct netr_LogonGetDomainInfo *r)
2061 p->rng_fault_state = true;
2062 return NT_STATUS_NOT_IMPLEMENTED;
2065 /****************************************************************
2066 ****************************************************************/
2068 WERROR _netr_ServerPasswordGet(struct pipes_struct *p,
2069 struct netr_ServerPasswordGet *r)
2071 p->rng_fault_state = true;
2072 return WERR_NOT_SUPPORTED;
2075 /****************************************************************
2076 ****************************************************************/
2078 WERROR _netr_NETRLOGONSENDTOSAM(struct pipes_struct *p,
2079 struct netr_NETRLOGONSENDTOSAM *r)
2081 p->rng_fault_state = true;
2082 return WERR_NOT_SUPPORTED;
2085 /****************************************************************
2086 ****************************************************************/
2088 WERROR _netr_DsRAddressToSitenamesW(struct pipes_struct *p,
2089 struct netr_DsRAddressToSitenamesW *r)
2091 p->rng_fault_state = true;
2092 return WERR_NOT_SUPPORTED;
2095 /****************************************************************
2096 ****************************************************************/
2098 WERROR _netr_DsRGetDCNameEx2(struct pipes_struct *p,
2099 struct netr_DsRGetDCNameEx2 *r)
2101 p->rng_fault_state = true;
2102 return WERR_NOT_SUPPORTED;
2105 /****************************************************************
2106 ****************************************************************/
2108 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(struct pipes_struct *p,
2109 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
2111 p->rng_fault_state = true;
2112 return WERR_NOT_SUPPORTED;
2115 /****************************************************************
2116 ****************************************************************/
2118 WERROR _netr_NetrEnumerateTrustedDomainsEx(struct pipes_struct *p,
2119 struct netr_NetrEnumerateTrustedDomainsEx *r)
2121 p->rng_fault_state = true;
2122 return WERR_NOT_SUPPORTED;
2125 /****************************************************************
2126 ****************************************************************/
2128 WERROR _netr_DsRAddressToSitenamesExW(struct pipes_struct *p,
2129 struct netr_DsRAddressToSitenamesExW *r)
2131 p->rng_fault_state = true;
2132 return WERR_NOT_SUPPORTED;
2135 /****************************************************************
2136 ****************************************************************/
2138 WERROR _netr_DsrGetDcSiteCoverageW(struct pipes_struct *p,
2139 struct netr_DsrGetDcSiteCoverageW *r)
2141 p->rng_fault_state = true;
2142 return WERR_NOT_SUPPORTED;
2145 /****************************************************************
2146 ****************************************************************/
2148 WERROR _netr_DsrEnumerateDomainTrusts(struct pipes_struct *p,
2149 struct netr_DsrEnumerateDomainTrusts *r)
2151 p->rng_fault_state = true;
2152 return WERR_NOT_SUPPORTED;
2155 /****************************************************************
2156 ****************************************************************/
2158 WERROR _netr_DsrDeregisterDNSHostRecords(struct pipes_struct *p,
2159 struct netr_DsrDeregisterDNSHostRecords *r)
2161 p->rng_fault_state = true;
2162 return WERR_NOT_SUPPORTED;
2165 /****************************************************************
2166 ****************************************************************/
2168 NTSTATUS _netr_ServerTrustPasswordsGet(struct pipes_struct *p,
2169 struct netr_ServerTrustPasswordsGet *r)
2171 p->rng_fault_state = true;
2172 return NT_STATUS_NOT_IMPLEMENTED;
2175 /****************************************************************
2176 ****************************************************************/
2178 WERROR _netr_DsRGetForestTrustInformation(struct pipes_struct *p,
2179 struct netr_DsRGetForestTrustInformation *r)
2181 p->rng_fault_state = true;
2182 return WERR_NOT_SUPPORTED;
2185 /****************************************************************
2186 ****************************************************************/
2188 static NTSTATUS fill_forest_trust_array(TALLOC_CTX *mem_ctx,
2189 struct lsa_ForestTrustInformation *info)
2191 struct lsa_ForestTrustRecord *e;
2192 struct pdb_domain_info *dom_info;
2193 struct lsa_ForestTrustDomainInfo *domain_info;
2195 dom_info = pdb_get_domain_info(mem_ctx);
2196 if (dom_info == NULL) {
2197 return NT_STATUS_NO_MEMORY;
2200 info->count = 2;
2201 info->entries = talloc_array(info, struct lsa_ForestTrustRecord *, 2);
2202 if (info->entries == NULL) {
2203 return NT_STATUS_NO_MEMORY;
2206 e = talloc(info, struct lsa_ForestTrustRecord);
2207 if (e == NULL) {
2208 return NT_STATUS_NO_MEMORY;
2211 e->flags = 0;
2212 e->type = LSA_FOREST_TRUST_TOP_LEVEL_NAME;
2213 e->time = 0; /* so far always 0 in trces. */
2214 e->forest_trust_data.top_level_name.string = talloc_steal(info,
2215 dom_info->dns_forest);
2217 info->entries[0] = e;
2219 e = talloc(info, struct lsa_ForestTrustRecord);
2220 if (e == NULL) {
2221 return NT_STATUS_NO_MEMORY;
2224 /* TODO: check if disabled and set flags accordingly */
2225 e->flags = 0;
2226 e->type = LSA_FOREST_TRUST_DOMAIN_INFO;
2227 e->time = 0; /* so far always 0 in traces. */
2229 domain_info = &e->forest_trust_data.domain_info;
2230 domain_info->domain_sid = dom_sid_dup(info, &dom_info->sid);
2232 domain_info->dns_domain_name.string = talloc_steal(info,
2233 dom_info->dns_domain);
2234 domain_info->netbios_domain_name.string = talloc_steal(info,
2235 dom_info->name);
2237 info->entries[1] = e;
2239 return NT_STATUS_OK;
2242 /****************************************************************
2243 _netr_GetForestTrustInformation
2244 ****************************************************************/
2246 NTSTATUS _netr_GetForestTrustInformation(struct pipes_struct *p,
2247 struct netr_GetForestTrustInformation *r)
2249 NTSTATUS status;
2250 struct netlogon_creds_CredentialState *creds;
2251 struct lsa_ForestTrustInformation *info, **info_ptr;
2253 /* TODO: check server name */
2255 status = schannel_check_creds_state(p->mem_ctx, lp_private_dir(),
2256 r->in.computer_name,
2257 r->in.credential,
2258 r->out.return_authenticator,
2259 &creds);
2260 if (!NT_STATUS_IS_OK(status)) {
2261 return status;
2264 if ((creds->secure_channel_type != SEC_CHAN_DNS_DOMAIN) &&
2265 (creds->secure_channel_type != SEC_CHAN_DOMAIN)) {
2266 return NT_STATUS_NOT_IMPLEMENTED;
2269 info_ptr = talloc(p->mem_ctx, struct lsa_ForestTrustInformation *);
2270 if (!info_ptr) {
2271 return NT_STATUS_NO_MEMORY;
2273 info = talloc_zero(info_ptr, struct lsa_ForestTrustInformation);
2274 if (!info) {
2275 return NT_STATUS_NO_MEMORY;
2278 status = fill_forest_trust_array(p->mem_ctx, info);
2279 if (!NT_STATUS_IS_OK(status)) {
2280 return status;
2283 *info_ptr = info;
2284 r->out.forest_trust_info = info_ptr;
2286 return NT_STATUS_OK;
2289 /****************************************************************
2290 ****************************************************************/
2292 static NTSTATUS get_password_from_trustAuth(TALLOC_CTX *mem_ctx,
2293 const DATA_BLOB *trustAuth_blob,
2294 const DATA_BLOB *session_key,
2295 struct samr_Password *current_pw_enc,
2296 struct samr_Password *previous_pw_enc)
2298 enum ndr_err_code ndr_err;
2299 struct trustAuthInOutBlob trustAuth;
2301 ndr_err = ndr_pull_struct_blob_all(trustAuth_blob, mem_ctx, &trustAuth,
2302 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2303 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2304 return NT_STATUS_UNSUCCESSFUL;
2308 if (trustAuth.count != 0 && trustAuth.current.count != 0 &&
2309 trustAuth.current.array[0].AuthType == TRUST_AUTH_TYPE_CLEAR) {
2310 mdfour(previous_pw_enc->hash,
2311 trustAuth.current.array[0].AuthInfo.clear.password,
2312 trustAuth.current.array[0].AuthInfo.clear.size);
2313 } else {
2314 return NT_STATUS_UNSUCCESSFUL;
2317 arcfour_crypt_blob(current_pw_enc->hash, sizeof(current_pw_enc->hash),
2318 session_key);
2320 if (trustAuth.previous.count != 0 &&
2321 trustAuth.previous.array[0].AuthType == TRUST_AUTH_TYPE_CLEAR) {
2322 mdfour(previous_pw_enc->hash,
2323 trustAuth.previous.array[0].AuthInfo.clear.password,
2324 trustAuth.previous.array[0].AuthInfo.clear.size);
2325 } else {
2326 mdfour(previous_pw_enc->hash, NULL, 0);
2328 arcfour_crypt_blob(previous_pw_enc->hash, sizeof(previous_pw_enc->hash),
2329 session_key);
2331 return NT_STATUS_OK;
2334 /****************************************************************
2335 _netr_ServerGetTrustInfo
2336 ****************************************************************/
2338 NTSTATUS _netr_ServerGetTrustInfo(struct pipes_struct *p,
2339 struct netr_ServerGetTrustInfo *r)
2341 NTSTATUS status;
2342 struct netlogon_creds_CredentialState *creds;
2343 char *account_name;
2344 size_t account_name_last;
2345 bool trusted;
2346 struct netr_TrustInfo *trust_info;
2347 struct pdb_trusted_domain *td;
2348 DATA_BLOB trustAuth_blob;
2349 struct samr_Password *new_owf_enc;
2350 struct samr_Password *old_owf_enc;
2351 DATA_BLOB session_key;
2353 /* TODO: check server name */
2355 status = schannel_check_creds_state(p->mem_ctx, lp_private_dir(),
2356 r->in.computer_name,
2357 r->in.credential,
2358 r->out.return_authenticator,
2359 &creds);
2360 if (!NT_STATUS_IS_OK(status)) {
2361 return status;
2364 account_name = talloc_strdup(p->mem_ctx, r->in.account_name);
2365 if (account_name == NULL) {
2366 return NT_STATUS_NO_MEMORY;
2369 account_name_last = strlen(account_name);
2370 if (account_name_last == 0) {
2371 return NT_STATUS_INVALID_PARAMETER;
2373 account_name_last--;
2374 if (account_name[account_name_last] == '.') {
2375 account_name[account_name_last] = '\0';
2378 if ((creds->secure_channel_type != SEC_CHAN_DNS_DOMAIN) &&
2379 (creds->secure_channel_type != SEC_CHAN_DOMAIN)) {
2380 trusted = false;
2381 } else {
2382 trusted = true;
2386 if (trusted) {
2387 account_name_last = strlen(account_name);
2388 if (account_name_last == 0) {
2389 return NT_STATUS_INVALID_PARAMETER;
2391 account_name_last--;
2392 if (account_name[account_name_last] == '$') {
2393 account_name[account_name_last] = '\0';
2396 status = pdb_get_trusted_domain(p->mem_ctx, account_name, &td);
2397 if (!NT_STATUS_IS_OK(status)) {
2398 return status;
2401 if (r->out.trust_info != NULL) {
2402 trust_info = talloc_zero(p->mem_ctx, struct netr_TrustInfo);
2403 if (trust_info == NULL) {
2404 return NT_STATUS_NO_MEMORY;
2406 trust_info->count = 1;
2408 trust_info->data = talloc_array(trust_info, uint32_t, 1);
2409 if (trust_info->data == NULL) {
2410 return NT_STATUS_NO_MEMORY;
2412 trust_info->data[0] = td->trust_attributes;
2414 *r->out.trust_info = trust_info;
2417 new_owf_enc = talloc_zero(p->mem_ctx, struct samr_Password);
2418 old_owf_enc = talloc_zero(p->mem_ctx, struct samr_Password);
2419 if (new_owf_enc == NULL || old_owf_enc == NULL) {
2420 return NT_STATUS_NO_MEMORY;
2423 /* TODO: which trustAuth shall we use if we have in/out trust or do they have to
2424 * be equal ? */
2425 if (td->trust_direction & NETR_TRUST_FLAG_INBOUND) {
2426 trustAuth_blob = td->trust_auth_incoming;
2427 } else if (td->trust_direction & NETR_TRUST_FLAG_OUTBOUND) {
2428 trustAuth_blob = td->trust_auth_outgoing;
2431 session_key.data = creds->session_key;
2432 session_key.length = sizeof(creds->session_key);
2433 status = get_password_from_trustAuth(p->mem_ctx, &trustAuth_blob,
2434 &session_key,
2435 new_owf_enc, old_owf_enc);
2437 if (!NT_STATUS_IS_OK(status)) {
2438 return status;
2441 r->out.new_owf_password = new_owf_enc;
2442 r->out.old_owf_password = old_owf_enc;
2443 } else {
2444 /* TODO: look for machine password */
2445 r->out.new_owf_password = NULL;
2446 r->out.old_owf_password = NULL;
2448 return NT_STATUS_NOT_IMPLEMENTED;
2451 return NT_STATUS_OK;
2454 /****************************************************************
2455 ****************************************************************/
2457 NTSTATUS _netr_Unused47(struct pipes_struct *p,
2458 struct netr_Unused47 *r)
2460 p->rng_fault_state = true;
2461 return NT_STATUS_NOT_IMPLEMENTED;
2464 /****************************************************************
2465 ****************************************************************/
2467 NTSTATUS _netr_DsrUpdateReadOnlyServerDnsRecords(struct pipes_struct *p,
2468 struct netr_DsrUpdateReadOnlyServerDnsRecords *r)
2470 p->rng_fault_state = true;
2471 return NT_STATUS_NOT_IMPLEMENTED;