s3:libsmb: add tstream_is_cli_np()
[Samba/gebeck_regimport.git] / source3 / rpc_server / srv_netlog_nt.c
blob05d6bd684c255240750bbf19c4a1d47735528677
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/cli_samr.h"
33 #include "../librpc/gen_ndr/cli_lsa.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"
41 extern userdom_struct current_user_info;
43 #undef DBGC_CLASS
44 #define DBGC_CLASS DBGC_RPC_SRV
46 struct netlogon_server_pipe_state {
47 struct netr_Credential client_challenge;
48 struct netr_Credential server_challenge;
51 /*************************************************************************
52 _netr_LogonControl
53 *************************************************************************/
55 WERROR _netr_LogonControl(struct pipes_struct *p,
56 struct netr_LogonControl *r)
58 struct netr_LogonControl2Ex l;
60 switch (r->in.level) {
61 case 1:
62 break;
63 case 2:
64 return WERR_NOT_SUPPORTED;
65 default:
66 return WERR_UNKNOWN_LEVEL;
69 l.in.logon_server = r->in.logon_server;
70 l.in.function_code = r->in.function_code;
71 l.in.level = r->in.level;
72 l.in.data = NULL;
73 l.out.query = r->out.query;
75 return _netr_LogonControl2Ex(p, &l);
78 /****************************************************************************
79 Send a message to smbd to do a sam synchronisation
80 **************************************************************************/
82 static void send_sync_message(struct messaging_context *msg_ctx)
84 DEBUG(3, ("sending sam synchronisation message\n"));
85 message_send_all(msg_ctx, MSG_SMB_SAM_SYNC, NULL, 0, NULL);
88 /*************************************************************************
89 _netr_LogonControl2
90 *************************************************************************/
92 WERROR _netr_LogonControl2(struct pipes_struct *p,
93 struct netr_LogonControl2 *r)
95 struct netr_LogonControl2Ex l;
97 l.in.logon_server = r->in.logon_server;
98 l.in.function_code = r->in.function_code;
99 l.in.level = r->in.level;
100 l.in.data = r->in.data;
101 l.out.query = r->out.query;
103 return _netr_LogonControl2Ex(p, &l);
106 /*************************************************************************
107 *************************************************************************/
109 static bool wb_change_trust_creds(const char *domain, WERROR *tc_status)
111 wbcErr result;
112 struct wbcAuthErrorInfo *error = NULL;
114 result = wbcChangeTrustCredentials(domain, &error);
115 switch (result) {
116 case WBC_ERR_WINBIND_NOT_AVAILABLE:
117 return false;
118 case WBC_ERR_DOMAIN_NOT_FOUND:
119 *tc_status = WERR_NO_SUCH_DOMAIN;
120 return true;
121 case WBC_ERR_SUCCESS:
122 *tc_status = WERR_OK;
123 return true;
124 default:
125 break;
128 if (error && error->nt_status != 0) {
129 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
130 } else {
131 *tc_status = WERR_TRUST_FAILURE;
133 wbcFreeMemory(error);
134 return true;
137 /*************************************************************************
138 *************************************************************************/
140 static bool wb_check_trust_creds(const char *domain, WERROR *tc_status)
142 wbcErr result;
143 struct wbcAuthErrorInfo *error = NULL;
145 result = wbcCheckTrustCredentials(domain, &error);
146 switch (result) {
147 case WBC_ERR_WINBIND_NOT_AVAILABLE:
148 return false;
149 case WBC_ERR_DOMAIN_NOT_FOUND:
150 *tc_status = WERR_NO_SUCH_DOMAIN;
151 return true;
152 case WBC_ERR_SUCCESS:
153 *tc_status = WERR_OK;
154 return true;
155 default:
156 break;
159 if (error && error->nt_status != 0) {
160 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
161 } else {
162 *tc_status = WERR_TRUST_FAILURE;
164 wbcFreeMemory(error);
165 return true;
168 /****************************************************************
169 _netr_LogonControl2Ex
170 ****************************************************************/
172 WERROR _netr_LogonControl2Ex(struct pipes_struct *p,
173 struct netr_LogonControl2Ex *r)
175 uint32_t flags = 0x0;
176 WERROR pdc_connection_status = WERR_OK;
177 uint32_t logon_attempts = 0x0;
178 WERROR tc_status;
179 fstring dc_name2;
180 const char *dc_name = NULL;
181 struct sockaddr_storage dc_ss;
182 const char *domain = NULL;
183 struct netr_NETLOGON_INFO_1 *info1;
184 struct netr_NETLOGON_INFO_2 *info2;
185 struct netr_NETLOGON_INFO_3 *info3;
186 struct netr_NETLOGON_INFO_4 *info4;
187 const char *fn;
188 uint32_t acct_ctrl;
190 switch (p->opnum) {
191 case NDR_NETR_LOGONCONTROL:
192 fn = "_netr_LogonControl";
193 break;
194 case NDR_NETR_LOGONCONTROL2:
195 fn = "_netr_LogonControl2";
196 break;
197 case NDR_NETR_LOGONCONTROL2EX:
198 fn = "_netr_LogonControl2Ex";
199 break;
200 default:
201 return WERR_INVALID_PARAM;
204 acct_ctrl = p->server_info->info3->base.acct_flags;
206 switch (r->in.function_code) {
207 case NETLOGON_CONTROL_TC_VERIFY:
208 case NETLOGON_CONTROL_CHANGE_PASSWORD:
209 case NETLOGON_CONTROL_REDISCOVER:
210 if ((geteuid() != sec_initial_uid()) &&
211 !nt_token_check_domain_rid(p->server_info->ptok, DOMAIN_RID_ADMINS) &&
212 !nt_token_check_sid(&global_sid_Builtin_Administrators, p->server_info->ptok) &&
213 !(acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST))) {
214 return WERR_ACCESS_DENIED;
216 break;
217 default:
218 break;
221 tc_status = WERR_NO_SUCH_DOMAIN;
223 switch (r->in.function_code) {
224 case NETLOGON_CONTROL_QUERY:
225 tc_status = WERR_OK;
226 break;
227 case NETLOGON_CONTROL_REPLICATE:
228 case NETLOGON_CONTROL_SYNCHRONIZE:
229 case NETLOGON_CONTROL_PDC_REPLICATE:
230 case NETLOGON_CONTROL_BACKUP_CHANGE_LOG:
231 case NETLOGON_CONTROL_BREAKPOINT:
232 if (acct_ctrl & ACB_NORMAL) {
233 return WERR_NOT_SUPPORTED;
234 } else if (acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST)) {
235 return WERR_ACCESS_DENIED;
236 } else {
237 return WERR_ACCESS_DENIED;
239 case NETLOGON_CONTROL_TRUNCATE_LOG:
240 if (acct_ctrl & ACB_NORMAL) {
241 break;
242 } else if (acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST)) {
243 return WERR_ACCESS_DENIED;
244 } else {
245 return WERR_ACCESS_DENIED;
248 case NETLOGON_CONTROL_TRANSPORT_NOTIFY:
249 case NETLOGON_CONTROL_FORCE_DNS_REG:
250 case NETLOGON_CONTROL_QUERY_DNS_REG:
251 return WERR_NOT_SUPPORTED;
252 case NETLOGON_CONTROL_FIND_USER:
253 if (!r->in.data || !r->in.data->user) {
254 return WERR_NOT_SUPPORTED;
256 break;
257 case NETLOGON_CONTROL_SET_DBFLAG:
258 if (!r->in.data) {
259 return WERR_NOT_SUPPORTED;
261 break;
262 case NETLOGON_CONTROL_TC_VERIFY:
263 if (!r->in.data || !r->in.data->domain) {
264 return WERR_NOT_SUPPORTED;
267 if (!wb_check_trust_creds(r->in.data->domain, &tc_status)) {
268 return WERR_NOT_SUPPORTED;
270 break;
271 case NETLOGON_CONTROL_TC_QUERY:
272 if (!r->in.data || !r->in.data->domain) {
273 return WERR_NOT_SUPPORTED;
276 domain = r->in.data->domain;
278 if (!is_trusted_domain(domain)) {
279 break;
282 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
283 tc_status = WERR_NO_LOGON_SERVERS;
284 break;
287 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
288 if (!dc_name) {
289 return WERR_NOMEM;
292 tc_status = WERR_OK;
294 break;
296 case NETLOGON_CONTROL_REDISCOVER:
297 if (!r->in.data || !r->in.data->domain) {
298 return WERR_NOT_SUPPORTED;
301 domain = r->in.data->domain;
303 if (!is_trusted_domain(domain)) {
304 break;
307 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
308 tc_status = WERR_NO_LOGON_SERVERS;
309 break;
312 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
313 if (!dc_name) {
314 return WERR_NOMEM;
317 tc_status = WERR_OK;
319 break;
321 case NETLOGON_CONTROL_CHANGE_PASSWORD:
322 if (!r->in.data || !r->in.data->domain) {
323 return WERR_NOT_SUPPORTED;
326 if (!wb_change_trust_creds(r->in.data->domain, &tc_status)) {
327 return WERR_NOT_SUPPORTED;
329 break;
331 default:
332 /* no idea what this should be */
333 DEBUG(0,("%s: unimplemented function level [%d]\n",
334 fn, r->in.function_code));
335 return WERR_UNKNOWN_LEVEL;
338 /* prepare the response */
340 switch (r->in.level) {
341 case 1:
342 info1 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_1);
343 W_ERROR_HAVE_NO_MEMORY(info1);
345 info1->flags = flags;
346 info1->pdc_connection_status = pdc_connection_status;
348 r->out.query->info1 = info1;
349 break;
350 case 2:
351 info2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_2);
352 W_ERROR_HAVE_NO_MEMORY(info2);
354 info2->flags = flags;
355 info2->pdc_connection_status = pdc_connection_status;
356 info2->trusted_dc_name = dc_name;
357 info2->tc_connection_status = tc_status;
359 r->out.query->info2 = info2;
360 break;
361 case 3:
362 info3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_3);
363 W_ERROR_HAVE_NO_MEMORY(info3);
365 info3->flags = flags;
366 info3->logon_attempts = logon_attempts;
368 r->out.query->info3 = info3;
369 break;
370 case 4:
371 info4 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_4);
372 W_ERROR_HAVE_NO_MEMORY(info4);
374 info4->trusted_dc_name = dc_name;
375 info4->trusted_domain_name = r->in.data->domain;
377 r->out.query->info4 = info4;
378 break;
379 default:
380 return WERR_UNKNOWN_LEVEL;
383 if (lp_server_role() == ROLE_DOMAIN_BDC) {
384 send_sync_message(p->msg_ctx);
387 return WERR_OK;
390 /*************************************************************************
391 _netr_NetrEnumerateTrustedDomains
392 *************************************************************************/
394 NTSTATUS _netr_NetrEnumerateTrustedDomains(struct pipes_struct *p,
395 struct netr_NetrEnumerateTrustedDomains *r)
397 NTSTATUS status;
398 DATA_BLOB blob;
399 int num_domains = 0;
400 const char **trusted_domains = NULL;
401 struct lsa_DomainList domain_list;
402 struct rpc_pipe_client *cli = NULL;
403 struct policy_handle pol;
404 uint32_t enum_ctx = 0;
405 int i;
406 uint32_t max_size = (uint32_t)-1;
408 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
410 status = rpc_pipe_open_internal(p->mem_ctx, &ndr_table_lsarpc.syntax_id,
411 p->server_info,
412 p->client_id,
413 p->msg_ctx,
414 &cli);
415 if (!NT_STATUS_IS_OK(status)) {
416 return status;
419 status = rpccli_lsa_open_policy2(cli, p->mem_ctx,
420 true,
421 LSA_POLICY_VIEW_LOCAL_INFORMATION,
422 &pol);
423 if (!NT_STATUS_IS_OK(status)) {
424 goto out;
427 do {
428 /* Lookup list of trusted domains */
430 status = rpccli_lsa_EnumTrustDom(cli, p->mem_ctx,
431 &pol,
432 &enum_ctx,
433 &domain_list,
434 max_size);
435 if (!NT_STATUS_IS_OK(status) &&
436 !NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES) &&
437 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
438 goto out;
441 for (i = 0; i < domain_list.count; i++) {
442 if (!add_string_to_array(p->mem_ctx, domain_list.domains[i].name.string,
443 &trusted_domains, &num_domains)) {
444 status = NT_STATUS_NO_MEMORY;
445 goto out;
448 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
450 if (num_domains > 0) {
451 /* multi sz terminate */
452 trusted_domains = talloc_realloc(p->mem_ctx, trusted_domains, const char *, num_domains + 1);
453 if (trusted_domains == NULL) {
454 status = NT_STATUS_NO_MEMORY;
455 goto out;
458 trusted_domains[num_domains] = NULL;
461 if (!push_reg_multi_sz(trusted_domains, &blob, trusted_domains)) {
462 TALLOC_FREE(trusted_domains);
463 status = NT_STATUS_NO_MEMORY;
464 goto out;
467 r->out.trusted_domains_blob->data = blob.data;
468 r->out.trusted_domains_blob->length = blob.length;
470 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
472 status = NT_STATUS_OK;
474 out:
475 if (cli && is_valid_policy_hnd(&pol)) {
476 rpccli_lsa_Close(cli, p->mem_ctx, &pol);
479 return status;
482 /*************************************************************************
483 *************************************************************************/
485 static NTSTATUS samr_find_machine_account(TALLOC_CTX *mem_ctx,
486 struct rpc_pipe_client *cli,
487 const char *account_name,
488 uint32_t access_mask,
489 struct dom_sid2 **domain_sid_p,
490 uint32_t *user_rid_p,
491 struct policy_handle *user_handle)
493 NTSTATUS status;
494 struct policy_handle connect_handle, domain_handle;
495 struct lsa_String domain_name;
496 struct dom_sid2 *domain_sid;
497 struct lsa_String names;
498 struct samr_Ids rids;
499 struct samr_Ids types;
500 uint32_t rid;
502 status = rpccli_samr_Connect2(cli, mem_ctx,
503 global_myname(),
504 SAMR_ACCESS_CONNECT_TO_SERVER |
505 SAMR_ACCESS_ENUM_DOMAINS |
506 SAMR_ACCESS_LOOKUP_DOMAIN,
507 &connect_handle);
508 if (!NT_STATUS_IS_OK(status)) {
509 goto out;
512 init_lsa_String(&domain_name, get_global_sam_name());
514 status = rpccli_samr_LookupDomain(cli, mem_ctx,
515 &connect_handle,
516 &domain_name,
517 &domain_sid);
518 if (!NT_STATUS_IS_OK(status)) {
519 goto out;
522 status = rpccli_samr_OpenDomain(cli, mem_ctx,
523 &connect_handle,
524 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
525 domain_sid,
526 &domain_handle);
527 if (!NT_STATUS_IS_OK(status)) {
528 goto out;
531 init_lsa_String(&names, account_name);
533 status = rpccli_samr_LookupNames(cli, mem_ctx,
534 &domain_handle,
536 &names,
537 &rids,
538 &types);
539 if (!NT_STATUS_IS_OK(status)) {
540 goto out;
543 if (rids.count != 1) {
544 status = NT_STATUS_NO_SUCH_USER;
545 goto out;
547 if (rids.count != types.count) {
548 status = NT_STATUS_INVALID_PARAMETER;
549 goto out;
551 if (types.ids[0] != SID_NAME_USER) {
552 status = NT_STATUS_NO_SUCH_USER;
553 goto out;
556 rid = rids.ids[0];
558 status = rpccli_samr_OpenUser(cli, mem_ctx,
559 &domain_handle,
560 access_mask,
561 rid,
562 user_handle);
563 if (!NT_STATUS_IS_OK(status)) {
564 goto out;
567 if (user_rid_p) {
568 *user_rid_p = rid;
571 if (domain_sid_p) {
572 *domain_sid_p = domain_sid;
575 out:
576 if (cli && is_valid_policy_hnd(&domain_handle)) {
577 rpccli_samr_Close(cli, mem_ctx, &domain_handle);
579 if (cli && is_valid_policy_hnd(&connect_handle)) {
580 rpccli_samr_Close(cli, mem_ctx, &connect_handle);
583 return status;
586 /******************************************************************
587 gets a machine password entry. checks access rights of the host.
588 ******************************************************************/
590 static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
591 enum netr_SchannelType sec_chan_type,
592 struct dom_sid *sid,
593 struct messaging_context *msg_ctx)
595 NTSTATUS status;
596 TALLOC_CTX *mem_ctx;
597 struct rpc_pipe_client *cli = NULL;
598 struct policy_handle user_handle;
599 uint32_t user_rid;
600 struct dom_sid *domain_sid;
601 uint32_t acct_ctrl;
602 union samr_UserInfo *info;
603 struct auth_serversupplied_info *server_info;
604 #if 0
607 * Currently this code is redundent as we already have a filter
608 * by hostname list. What this code really needs to do is to
609 * get a hosts allowed/hosts denied list from the SAM database
610 * on a per user basis, and make the access decision there.
611 * I will leave this code here for now as a reminder to implement
612 * this at a later date. JRA.
615 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
616 p->client_id.name,
617 p->client_id.addr)) {
618 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
619 return False;
621 #endif /* 0 */
623 mem_ctx = talloc_new(talloc_tos());
624 if (mem_ctx == NULL) {
625 status = NT_STATUS_NO_MEMORY;
626 goto out;
629 status = make_server_info_system(mem_ctx, &server_info);
630 if (!NT_STATUS_IS_OK(status)) {
631 goto out;
634 ZERO_STRUCT(user_handle);
636 status = rpc_pipe_open_internal(mem_ctx, &ndr_table_samr.syntax_id,
637 server_info, NULL, msg_ctx,
638 &cli);
639 if (!NT_STATUS_IS_OK(status)) {
640 goto out;
643 become_root();
644 status = samr_find_machine_account(mem_ctx, cli, mach_acct,
645 SEC_FLAG_MAXIMUM_ALLOWED,
646 &domain_sid, &user_rid,
647 &user_handle);
648 unbecome_root();
649 if (!NT_STATUS_IS_OK(status)) {
650 goto out;
653 status = rpccli_samr_QueryUserInfo2(cli, mem_ctx,
654 &user_handle,
655 UserControlInformation,
656 &info);
657 if (!NT_STATUS_IS_OK(status)) {
658 goto out;
661 acct_ctrl = info->info16.acct_flags;
663 if (acct_ctrl & ACB_DISABLED) {
664 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
665 status = NT_STATUS_ACCOUNT_DISABLED;
666 goto out;
669 if (!(acct_ctrl & ACB_SVRTRUST) &&
670 !(acct_ctrl & ACB_WSTRUST) &&
671 !(acct_ctrl & ACB_DOMTRUST))
673 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
674 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
675 goto out;
678 switch (sec_chan_type) {
679 case SEC_CHAN_BDC:
680 if (!(acct_ctrl & ACB_SVRTRUST)) {
681 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
682 "but not a server trust account\n", mach_acct));
683 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
684 goto out;
686 break;
687 case SEC_CHAN_WKSTA:
688 if (!(acct_ctrl & ACB_WSTRUST)) {
689 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
690 "but not a workstation trust account\n", mach_acct));
691 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
692 goto out;
694 break;
695 case SEC_CHAN_DOMAIN:
696 if (!(acct_ctrl & ACB_DOMTRUST)) {
697 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
698 "but not a interdomain trust account\n", mach_acct));
699 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
700 goto out;
702 break;
703 default:
704 break;
707 become_root();
708 status = rpccli_samr_QueryUserInfo2(cli, mem_ctx,
709 &user_handle,
710 UserInternal1Information,
711 &info);
712 unbecome_root();
713 if (!NT_STATUS_IS_OK(status)) {
714 goto out;
716 if (info->info18.nt_pwd_active == 0) {
717 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
718 status = NT_STATUS_LOGON_FAILURE;
719 goto out;
722 /* samr gives out nthash unencrypted (!) */
723 memcpy(md4pw->hash, info->info18.nt_pwd.hash, 16);
725 sid_compose(sid, domain_sid, user_rid);
727 out:
728 if (cli && is_valid_policy_hnd(&user_handle)) {
729 rpccli_samr_Close(cli, mem_ctx, &user_handle);
732 talloc_free(mem_ctx);
734 return status;
737 /*************************************************************************
738 _netr_ServerReqChallenge
739 *************************************************************************/
741 NTSTATUS _netr_ServerReqChallenge(struct pipes_struct *p,
742 struct netr_ServerReqChallenge *r)
744 struct netlogon_server_pipe_state *pipe_state =
745 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
747 if (pipe_state) {
748 DEBUG(10,("_netr_ServerReqChallenge: new challenge requested. Clearing old state.\n"));
749 talloc_free(pipe_state);
750 p->private_data = NULL;
753 pipe_state = talloc(p, struct netlogon_server_pipe_state);
754 NT_STATUS_HAVE_NO_MEMORY(pipe_state);
756 pipe_state->client_challenge = *r->in.credentials;
758 generate_random_buffer(pipe_state->server_challenge.data,
759 sizeof(pipe_state->server_challenge.data));
761 *r->out.return_credentials = pipe_state->server_challenge;
763 p->private_data = pipe_state;
765 return NT_STATUS_OK;
768 /*************************************************************************
769 _netr_ServerAuthenticate
770 Create the initial credentials.
771 *************************************************************************/
773 NTSTATUS _netr_ServerAuthenticate(struct pipes_struct *p,
774 struct netr_ServerAuthenticate *r)
776 struct netr_ServerAuthenticate3 a;
777 uint32_t negotiate_flags = 0;
778 uint32_t rid;
780 a.in.server_name = r->in.server_name;
781 a.in.account_name = r->in.account_name;
782 a.in.secure_channel_type = r->in.secure_channel_type;
783 a.in.computer_name = r->in.computer_name;
784 a.in.credentials = r->in.credentials;
785 a.in.negotiate_flags = &negotiate_flags;
787 a.out.return_credentials = r->out.return_credentials;
788 a.out.rid = &rid;
789 a.out.negotiate_flags = &negotiate_flags;
791 return _netr_ServerAuthenticate3(p, &a);
795 /*************************************************************************
796 _netr_ServerAuthenticate3
797 *************************************************************************/
799 NTSTATUS _netr_ServerAuthenticate3(struct pipes_struct *p,
800 struct netr_ServerAuthenticate3 *r)
802 NTSTATUS status;
803 uint32_t srv_flgs;
804 /* r->in.negotiate_flags is an aliased pointer to r->out.negotiate_flags,
805 * so use a copy to avoid destroying the client values. */
806 uint32_t in_neg_flags = *r->in.negotiate_flags;
807 const char *fn;
808 struct dom_sid sid;
809 struct samr_Password mach_pwd;
810 struct netlogon_creds_CredentialState *creds;
811 struct netlogon_server_pipe_state *pipe_state =
812 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
814 /* According to Microsoft (see bugid #6099)
815 * Windows 7 looks at the negotiate_flags
816 * returned in this structure *even if the
817 * call fails with access denied* ! So in order
818 * to allow Win7 to connect to a Samba NT style
819 * PDC we set the flags before we know if it's
820 * an error or not.
823 /* 0x000001ff */
824 srv_flgs = NETLOGON_NEG_ACCOUNT_LOCKOUT |
825 NETLOGON_NEG_PERSISTENT_SAMREPL |
826 NETLOGON_NEG_ARCFOUR |
827 NETLOGON_NEG_PROMOTION_COUNT |
828 NETLOGON_NEG_CHANGELOG_BDC |
829 NETLOGON_NEG_FULL_SYNC_REPL |
830 NETLOGON_NEG_MULTIPLE_SIDS |
831 NETLOGON_NEG_REDO |
832 NETLOGON_NEG_PASSWORD_CHANGE_REFUSAL |
833 NETLOGON_NEG_PASSWORD_SET2;
835 /* Ensure we support strong (128-bit) keys. */
836 if (in_neg_flags & NETLOGON_NEG_STRONG_KEYS) {
837 srv_flgs |= NETLOGON_NEG_STRONG_KEYS;
840 if (lp_server_schannel() != false) {
841 srv_flgs |= NETLOGON_NEG_SCHANNEL;
844 switch (p->opnum) {
845 case NDR_NETR_SERVERAUTHENTICATE:
846 fn = "_netr_ServerAuthenticate";
847 break;
848 case NDR_NETR_SERVERAUTHENTICATE2:
849 fn = "_netr_ServerAuthenticate2";
850 break;
851 case NDR_NETR_SERVERAUTHENTICATE3:
852 fn = "_netr_ServerAuthenticate3";
853 break;
854 default:
855 return NT_STATUS_INTERNAL_ERROR;
858 /* We use this as the key to store the creds: */
859 /* r->in.computer_name */
861 if (!pipe_state) {
862 DEBUG(0,("%s: no challenge sent to client %s\n", fn,
863 r->in.computer_name));
864 status = NT_STATUS_ACCESS_DENIED;
865 goto out;
868 if ( (lp_server_schannel() == true) &&
869 ((in_neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
871 /* schannel must be used, but client did not offer it. */
872 DEBUG(0,("%s: schannel required but client failed "
873 "to offer it. Client was %s\n",
874 fn, r->in.account_name));
875 status = NT_STATUS_ACCESS_DENIED;
876 goto out;
879 status = get_md4pw(&mach_pwd,
880 r->in.account_name,
881 r->in.secure_channel_type,
882 &sid, p->msg_ctx);
883 if (!NT_STATUS_IS_OK(status)) {
884 DEBUG(0,("%s: failed to get machine password for "
885 "account %s: %s\n",
886 fn, r->in.account_name, nt_errstr(status) ));
887 /* always return NT_STATUS_ACCESS_DENIED */
888 status = NT_STATUS_ACCESS_DENIED;
889 goto out;
892 /* From the client / server challenges and md4 password, generate sess key */
893 /* Check client credentials are valid. */
894 creds = netlogon_creds_server_init(p->mem_ctx,
895 r->in.account_name,
896 r->in.computer_name,
897 r->in.secure_channel_type,
898 &pipe_state->client_challenge,
899 &pipe_state->server_challenge,
900 &mach_pwd,
901 r->in.credentials,
902 r->out.return_credentials,
903 *r->in.negotiate_flags);
904 if (!creds) {
905 DEBUG(0,("%s: netlogon_creds_server_check failed. Rejecting auth "
906 "request from client %s machine account %s\n",
907 fn, r->in.computer_name,
908 r->in.account_name));
909 status = NT_STATUS_ACCESS_DENIED;
910 goto out;
913 creds->sid = dom_sid_dup(creds, &sid);
914 if (!creds->sid) {
915 status = NT_STATUS_NO_MEMORY;
916 goto out;
919 /* Store off the state so we can continue after client disconnect. */
920 become_root();
921 status = schannel_save_creds_state(p->mem_ctx, lp_private_dir(), creds);
922 unbecome_root();
924 if (!NT_STATUS_IS_OK(status)) {
925 goto out;
928 sid_peek_rid(&sid, r->out.rid);
930 status = NT_STATUS_OK;
932 out:
934 *r->out.negotiate_flags = srv_flgs;
935 return status;
938 /*************************************************************************
939 _netr_ServerAuthenticate2
940 *************************************************************************/
942 NTSTATUS _netr_ServerAuthenticate2(struct pipes_struct *p,
943 struct netr_ServerAuthenticate2 *r)
945 struct netr_ServerAuthenticate3 a;
946 uint32_t rid;
948 a.in.server_name = r->in.server_name;
949 a.in.account_name = r->in.account_name;
950 a.in.secure_channel_type = r->in.secure_channel_type;
951 a.in.computer_name = r->in.computer_name;
952 a.in.credentials = r->in.credentials;
953 a.in.negotiate_flags = r->in.negotiate_flags;
955 a.out.return_credentials = r->out.return_credentials;
956 a.out.rid = &rid;
957 a.out.negotiate_flags = r->out.negotiate_flags;
959 return _netr_ServerAuthenticate3(p, &a);
962 /*************************************************************************
963 * If schannel is required for this call test that it actually is available.
964 *************************************************************************/
965 static NTSTATUS schannel_check_required(struct pipe_auth_data *auth_info,
966 const char *computer_name,
967 bool integrity, bool privacy)
969 if (auth_info && auth_info->auth_type == DCERPC_AUTH_TYPE_SCHANNEL) {
970 if (!privacy && !integrity) {
971 return NT_STATUS_OK;
974 if ((!privacy && integrity) &&
975 auth_info->auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
976 return NT_STATUS_OK;
979 if ((privacy || integrity) &&
980 auth_info->auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
981 return NT_STATUS_OK;
985 /* test didn't pass */
986 DEBUG(0, ("schannel_check_required: [%s] is not using schannel\n",
987 computer_name));
989 return NT_STATUS_ACCESS_DENIED;
992 /*************************************************************************
993 *************************************************************************/
995 static NTSTATUS netr_creds_server_step_check(struct pipes_struct *p,
996 TALLOC_CTX *mem_ctx,
997 const char *computer_name,
998 struct netr_Authenticator *received_authenticator,
999 struct netr_Authenticator *return_authenticator,
1000 struct netlogon_creds_CredentialState **creds_out)
1002 NTSTATUS status;
1003 bool schannel_global_required = (lp_server_schannel() == true) ? true:false;
1005 if (schannel_global_required) {
1006 status = schannel_check_required(&p->auth,
1007 computer_name,
1008 false, false);
1009 if (!NT_STATUS_IS_OK(status)) {
1010 return status;
1014 status = schannel_check_creds_state(mem_ctx, lp_private_dir(),
1015 computer_name, received_authenticator,
1016 return_authenticator, creds_out);
1018 return status;
1021 /*************************************************************************
1022 *************************************************************************/
1024 static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
1025 struct auth_serversupplied_info *server_info,
1026 struct messaging_context *msg_ctx,
1027 const char *account_name,
1028 struct samr_Password *nt_hash)
1030 NTSTATUS status;
1031 struct rpc_pipe_client *cli = NULL;
1032 struct policy_handle user_handle;
1033 uint32_t acct_ctrl;
1034 union samr_UserInfo *info;
1035 struct samr_UserInfo18 info18;
1036 DATA_BLOB in,out;
1038 ZERO_STRUCT(user_handle);
1040 status = rpc_pipe_open_internal(mem_ctx, &ndr_table_samr.syntax_id,
1041 server_info, NULL, msg_ctx,
1042 &cli);
1043 if (!NT_STATUS_IS_OK(status)) {
1044 goto out;
1047 status = samr_find_machine_account(mem_ctx, cli, account_name,
1048 SEC_FLAG_MAXIMUM_ALLOWED,
1049 NULL, NULL,
1050 &user_handle);
1051 if (!NT_STATUS_IS_OK(status)) {
1052 goto out;
1055 status = rpccli_samr_QueryUserInfo2(cli, mem_ctx,
1056 &user_handle,
1057 UserControlInformation,
1058 &info);
1059 if (!NT_STATUS_IS_OK(status)) {
1060 goto out;
1063 acct_ctrl = info->info16.acct_flags;
1065 if (!(acct_ctrl & ACB_WSTRUST ||
1066 acct_ctrl & ACB_SVRTRUST ||
1067 acct_ctrl & ACB_DOMTRUST)) {
1068 status = NT_STATUS_NO_SUCH_USER;
1069 goto out;
1072 if (acct_ctrl & ACB_DISABLED) {
1073 status = NT_STATUS_ACCOUNT_DISABLED;
1074 goto out;
1077 ZERO_STRUCT(info18);
1079 in = data_blob_const(nt_hash->hash, 16);
1080 out = data_blob_talloc_zero(mem_ctx, 16);
1081 sess_crypt_blob(&out, &in, &server_info->user_session_key, true);
1082 memcpy(info18.nt_pwd.hash, out.data, out.length);
1084 info18.nt_pwd_active = true;
1086 info->info18 = info18;
1088 status = rpccli_samr_SetUserInfo2(cli, mem_ctx,
1089 &user_handle,
1090 UserInternal1Information,
1091 info);
1092 if (!NT_STATUS_IS_OK(status)) {
1093 goto out;
1096 out:
1097 if (cli && is_valid_policy_hnd(&user_handle)) {
1098 rpccli_samr_Close(cli, mem_ctx, &user_handle);
1101 return status;
1104 /*************************************************************************
1105 _netr_ServerPasswordSet
1106 *************************************************************************/
1108 NTSTATUS _netr_ServerPasswordSet(struct pipes_struct *p,
1109 struct netr_ServerPasswordSet *r)
1111 NTSTATUS status = NT_STATUS_OK;
1112 int i;
1113 struct netlogon_creds_CredentialState *creds;
1115 DEBUG(5,("_netr_ServerPasswordSet: %d\n", __LINE__));
1117 become_root();
1118 status = netr_creds_server_step_check(p, p->mem_ctx,
1119 r->in.computer_name,
1120 r->in.credential,
1121 r->out.return_authenticator,
1122 &creds);
1123 unbecome_root();
1125 if (!NT_STATUS_IS_OK(status)) {
1126 DEBUG(2,("_netr_ServerPasswordSet: netlogon_creds_server_step failed. Rejecting auth "
1127 "request from client %s machine account %s\n",
1128 r->in.computer_name, creds->computer_name));
1129 TALLOC_FREE(creds);
1130 return status;
1133 DEBUG(3,("_netr_ServerPasswordSet: Server Password Set by remote machine:[%s] on account [%s]\n",
1134 r->in.computer_name, creds->computer_name));
1136 netlogon_creds_des_decrypt(creds, r->in.new_password);
1138 DEBUG(100,("_netr_ServerPasswordSet: new given value was :\n"));
1139 for(i = 0; i < sizeof(r->in.new_password->hash); i++)
1140 DEBUG(100,("%02X ", r->in.new_password->hash[i]));
1141 DEBUG(100,("\n"));
1143 status = netr_set_machine_account_password(p->mem_ctx,
1144 p->server_info,
1145 p->msg_ctx,
1146 creds->account_name,
1147 r->in.new_password);
1148 return status;
1151 /****************************************************************
1152 _netr_ServerPasswordSet2
1153 ****************************************************************/
1155 NTSTATUS _netr_ServerPasswordSet2(struct pipes_struct *p,
1156 struct netr_ServerPasswordSet2 *r)
1158 NTSTATUS status;
1159 struct netlogon_creds_CredentialState *creds;
1160 DATA_BLOB plaintext;
1161 struct samr_CryptPassword password_buf;
1162 struct samr_Password nt_hash;
1164 become_root();
1165 status = netr_creds_server_step_check(p, p->mem_ctx,
1166 r->in.computer_name,
1167 r->in.credential,
1168 r->out.return_authenticator,
1169 &creds);
1170 unbecome_root();
1172 if (!NT_STATUS_IS_OK(status)) {
1173 DEBUG(2,("_netr_ServerPasswordSet2: netlogon_creds_server_step "
1174 "failed. Rejecting auth request from client %s machine account %s\n",
1175 r->in.computer_name, creds->computer_name));
1176 TALLOC_FREE(creds);
1177 return status;
1180 memcpy(password_buf.data, r->in.new_password->data, 512);
1181 SIVAL(password_buf.data, 512, r->in.new_password->length);
1182 netlogon_creds_arcfour_crypt(creds, password_buf.data, 516);
1184 if (!extract_pw_from_buffer(p->mem_ctx, password_buf.data, &plaintext)) {
1185 return NT_STATUS_WRONG_PASSWORD;
1188 mdfour(nt_hash.hash, plaintext.data, plaintext.length);
1190 status = netr_set_machine_account_password(p->mem_ctx,
1191 p->server_info,
1192 p->msg_ctx,
1193 creds->account_name,
1194 &nt_hash);
1195 return status;
1198 /*************************************************************************
1199 _netr_LogonSamLogoff
1200 *************************************************************************/
1202 NTSTATUS _netr_LogonSamLogoff(struct pipes_struct *p,
1203 struct netr_LogonSamLogoff *r)
1205 NTSTATUS status;
1206 struct netlogon_creds_CredentialState *creds;
1208 become_root();
1209 status = netr_creds_server_step_check(p, p->mem_ctx,
1210 r->in.computer_name,
1211 r->in.credential,
1212 r->out.return_authenticator,
1213 &creds);
1214 unbecome_root();
1216 return status;
1219 /*************************************************************************
1220 _netr_LogonSamLogon_base
1221 *************************************************************************/
1223 static NTSTATUS _netr_LogonSamLogon_base(struct pipes_struct *p,
1224 struct netr_LogonSamLogonEx *r,
1225 struct netlogon_creds_CredentialState *creds)
1227 NTSTATUS status = NT_STATUS_OK;
1228 union netr_LogonLevel *logon = r->in.logon;
1229 const char *nt_username, *nt_domain, *nt_workstation;
1230 struct auth_usersupplied_info *user_info = NULL;
1231 struct auth_serversupplied_info *server_info = NULL;
1232 struct auth_context *auth_context = NULL;
1233 uint8_t pipe_session_key[16];
1234 bool process_creds = true;
1235 const char *fn;
1237 switch (p->opnum) {
1238 case NDR_NETR_LOGONSAMLOGON:
1239 process_creds = true;
1240 fn = "_netr_LogonSamLogon";
1241 break;
1242 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
1243 process_creds = true;
1244 fn = "_netr_LogonSamLogonWithFlags";
1245 break;
1246 case NDR_NETR_LOGONSAMLOGONEX:
1247 process_creds = false;
1248 fn = "_netr_LogonSamLogonEx";
1249 break;
1250 default:
1251 return NT_STATUS_INTERNAL_ERROR;
1254 *r->out.authoritative = true; /* authoritative response */
1256 switch (r->in.validation_level) {
1257 case 2:
1258 r->out.validation->sam2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo2);
1259 if (!r->out.validation->sam2) {
1260 return NT_STATUS_NO_MEMORY;
1262 break;
1263 case 3:
1264 r->out.validation->sam3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo3);
1265 if (!r->out.validation->sam3) {
1266 return NT_STATUS_NO_MEMORY;
1268 break;
1269 case 6:
1270 r->out.validation->sam6 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo6);
1271 if (!r->out.validation->sam6) {
1272 return NT_STATUS_NO_MEMORY;
1274 break;
1275 default:
1276 DEBUG(0,("%s: bad validation_level value %d.\n",
1277 fn, (int)r->in.validation_level));
1278 return NT_STATUS_INVALID_INFO_CLASS;
1281 switch (r->in.logon_level) {
1282 case NetlogonInteractiveInformation:
1283 case NetlogonServiceInformation:
1284 case NetlogonInteractiveTransitiveInformation:
1285 case NetlogonServiceTransitiveInformation:
1286 nt_username = logon->password->identity_info.account_name.string ?
1287 logon->password->identity_info.account_name.string : "";
1288 nt_domain = logon->password->identity_info.domain_name.string ?
1289 logon->password->identity_info.domain_name.string : "";
1290 nt_workstation = logon->password->identity_info.workstation.string ?
1291 logon->password->identity_info.workstation.string : "";
1293 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
1294 break;
1295 case NetlogonNetworkInformation:
1296 case NetlogonNetworkTransitiveInformation:
1297 nt_username = logon->network->identity_info.account_name.string ?
1298 logon->network->identity_info.account_name.string : "";
1299 nt_domain = logon->network->identity_info.domain_name.string ?
1300 logon->network->identity_info.domain_name.string : "";
1301 nt_workstation = logon->network->identity_info.workstation.string ?
1302 logon->network->identity_info.workstation.string : "";
1304 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
1305 break;
1306 default:
1307 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1308 return NT_STATUS_INVALID_INFO_CLASS;
1309 } /* end switch */
1311 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
1312 fstrcpy(current_user_info.smb_name, nt_username);
1313 sub_set_smb_name(nt_username);
1315 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
1316 r->in.validation_level, nt_username));
1318 status = NT_STATUS_OK;
1320 switch (r->in.logon_level) {
1321 case NetlogonNetworkInformation:
1322 case NetlogonNetworkTransitiveInformation:
1324 const char *wksname = nt_workstation;
1326 status = make_auth_context_fixed(talloc_tos(), &auth_context,
1327 logon->network->challenge);
1328 if (!NT_STATUS_IS_OK(status)) {
1329 return status;
1332 /* For a network logon, the workstation name comes in with two
1333 * backslashes in the front. Strip them if they are there. */
1335 if (*wksname == '\\') wksname++;
1336 if (*wksname == '\\') wksname++;
1338 /* Standard challenge/response authenticaion */
1339 if (!make_user_info_netlogon_network(&user_info,
1340 nt_username, nt_domain,
1341 wksname,
1342 logon->network->identity_info.parameter_control,
1343 logon->network->lm.data,
1344 logon->network->lm.length,
1345 logon->network->nt.data,
1346 logon->network->nt.length)) {
1347 status = NT_STATUS_NO_MEMORY;
1349 break;
1351 case NetlogonInteractiveInformation:
1352 case NetlogonServiceInformation:
1353 case NetlogonInteractiveTransitiveInformation:
1354 case NetlogonServiceTransitiveInformation:
1356 /* 'Interactive' authentication, supplies the password in its
1357 MD4 form, encrypted with the session key. We will convert
1358 this to challenge/response for the auth subsystem to chew
1359 on */
1361 uint8_t chal[8];
1363 status = make_auth_context_subsystem(talloc_tos(),
1364 &auth_context);
1365 if (!NT_STATUS_IS_OK(status)) {
1366 return status;
1369 auth_context->get_ntlm_challenge(auth_context, chal);
1371 if (!make_user_info_netlogon_interactive(&user_info,
1372 nt_username, nt_domain,
1373 nt_workstation,
1374 logon->password->identity_info.parameter_control,
1375 chal,
1376 logon->password->lmpassword.hash,
1377 logon->password->ntpassword.hash,
1378 creds->session_key)) {
1379 status = NT_STATUS_NO_MEMORY;
1381 break;
1383 default:
1384 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1385 return NT_STATUS_INVALID_INFO_CLASS;
1386 } /* end switch */
1388 if ( NT_STATUS_IS_OK(status) ) {
1389 status = auth_context->check_ntlm_password(auth_context,
1390 user_info, &server_info);
1393 TALLOC_FREE(auth_context);
1394 free_user_info(&user_info);
1396 DEBUG(5,("%s: check_password returned status %s\n",
1397 fn, nt_errstr(status)));
1399 /* Check account and password */
1401 if (!NT_STATUS_IS_OK(status)) {
1402 /* If we don't know what this domain is, we need to
1403 indicate that we are not authoritative. This
1404 allows the client to decide if it needs to try
1405 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
1406 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1407 && !strequal(nt_domain, get_global_sam_name())
1408 && !is_trusted_domain(nt_domain) )
1409 *r->out.authoritative = false; /* We are not authoritative */
1411 TALLOC_FREE(server_info);
1412 return status;
1415 if (server_info->guest) {
1416 /* We don't like guest domain logons... */
1417 DEBUG(5,("%s: Attempted domain logon as GUEST "
1418 "denied.\n", fn));
1419 TALLOC_FREE(server_info);
1420 return NT_STATUS_LOGON_FAILURE;
1423 /* This is the point at which, if the login was successful, that
1424 the SAM Local Security Authority should record that the user is
1425 logged in to the domain. */
1427 if (process_creds) {
1428 /* Get the pipe session key from the creds. */
1429 memcpy(pipe_session_key, creds->session_key, 16);
1430 } else {
1431 struct schannel_state *schannel_auth;
1432 /* Get the pipe session key from the schannel. */
1433 if ((p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL)
1434 || (p->auth.auth_ctx == NULL)) {
1435 return NT_STATUS_INVALID_HANDLE;
1438 schannel_auth = talloc_get_type_abort(p->auth.auth_ctx,
1439 struct schannel_state);
1440 memcpy(pipe_session_key, schannel_auth->creds->session_key, 16);
1443 switch (r->in.validation_level) {
1444 case 2:
1445 status = serverinfo_to_SamInfo2(server_info, pipe_session_key, 16,
1446 r->out.validation->sam2);
1447 break;
1448 case 3:
1449 status = serverinfo_to_SamInfo3(server_info, pipe_session_key, 16,
1450 r->out.validation->sam3);
1451 break;
1452 case 6:
1453 status = serverinfo_to_SamInfo6(server_info, pipe_session_key, 16,
1454 r->out.validation->sam6);
1455 break;
1458 TALLOC_FREE(server_info);
1460 return status;
1463 /****************************************************************
1464 _netr_LogonSamLogonWithFlags
1465 ****************************************************************/
1467 NTSTATUS _netr_LogonSamLogonWithFlags(struct pipes_struct *p,
1468 struct netr_LogonSamLogonWithFlags *r)
1470 NTSTATUS status;
1471 struct netlogon_creds_CredentialState *creds;
1472 struct netr_LogonSamLogonEx r2;
1473 struct netr_Authenticator return_authenticator;
1475 become_root();
1476 status = netr_creds_server_step_check(p, p->mem_ctx,
1477 r->in.computer_name,
1478 r->in.credential,
1479 &return_authenticator,
1480 &creds);
1481 unbecome_root();
1482 if (!NT_STATUS_IS_OK(status)) {
1483 return status;
1486 r2.in.server_name = r->in.server_name;
1487 r2.in.computer_name = r->in.computer_name;
1488 r2.in.logon_level = r->in.logon_level;
1489 r2.in.logon = r->in.logon;
1490 r2.in.validation_level = r->in.validation_level;
1491 r2.in.flags = r->in.flags;
1492 r2.out.validation = r->out.validation;
1493 r2.out.authoritative = r->out.authoritative;
1494 r2.out.flags = r->out.flags;
1496 status = _netr_LogonSamLogon_base(p, &r2, creds);
1498 *r->out.return_authenticator = return_authenticator;
1500 return status;
1503 /*************************************************************************
1504 _netr_LogonSamLogon
1505 *************************************************************************/
1507 NTSTATUS _netr_LogonSamLogon(struct pipes_struct *p,
1508 struct netr_LogonSamLogon *r)
1510 NTSTATUS status;
1511 struct netr_LogonSamLogonWithFlags r2;
1512 uint32_t flags = 0;
1514 r2.in.server_name = r->in.server_name;
1515 r2.in.computer_name = r->in.computer_name;
1516 r2.in.credential = r->in.credential;
1517 r2.in.logon_level = r->in.logon_level;
1518 r2.in.logon = r->in.logon;
1519 r2.in.validation_level = r->in.validation_level;
1520 r2.in.return_authenticator = r->in.return_authenticator;
1521 r2.in.flags = &flags;
1522 r2.out.validation = r->out.validation;
1523 r2.out.authoritative = r->out.authoritative;
1524 r2.out.flags = &flags;
1525 r2.out.return_authenticator = r->out.return_authenticator;
1527 status = _netr_LogonSamLogonWithFlags(p, &r2);
1529 return status;
1532 /*************************************************************************
1533 _netr_LogonSamLogonEx
1534 - no credential chaining. Map into net sam logon.
1535 *************************************************************************/
1537 NTSTATUS _netr_LogonSamLogonEx(struct pipes_struct *p,
1538 struct netr_LogonSamLogonEx *r)
1540 NTSTATUS status;
1541 struct netlogon_creds_CredentialState *creds = NULL;
1543 become_root();
1544 status = schannel_get_creds_state(p->mem_ctx, lp_private_dir(),
1545 r->in.computer_name, &creds);
1546 unbecome_root();
1547 if (!NT_STATUS_IS_OK(status)) {
1548 return status;
1551 /* Only allow this if the pipe is protected. */
1552 if (p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
1553 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1554 get_remote_machine_name() ));
1555 return NT_STATUS_INVALID_PARAMETER;
1558 status = _netr_LogonSamLogon_base(p, r, creds);
1559 TALLOC_FREE(creds);
1561 return status;
1564 /*************************************************************************
1565 _ds_enum_dom_trusts
1566 *************************************************************************/
1567 #if 0 /* JERRY -- not correct */
1568 NTSTATUS _ds_enum_dom_trusts(struct pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1569 DS_R_ENUM_DOM_TRUSTS *r_u)
1571 NTSTATUS status = NT_STATUS_OK;
1573 /* TODO: According to MSDN, the can only be executed against a
1574 DC or domain member running Windows 2000 or later. Need
1575 to test against a standalone 2k server and see what it
1576 does. A windows 2000 DC includes its own domain in the
1577 list. --jerry */
1579 return status;
1581 #endif /* JERRY */
1584 /****************************************************************
1585 ****************************************************************/
1587 WERROR _netr_LogonUasLogon(struct pipes_struct *p,
1588 struct netr_LogonUasLogon *r)
1590 p->rng_fault_state = true;
1591 return WERR_NOT_SUPPORTED;
1594 /****************************************************************
1595 ****************************************************************/
1597 WERROR _netr_LogonUasLogoff(struct pipes_struct *p,
1598 struct netr_LogonUasLogoff *r)
1600 p->rng_fault_state = true;
1601 return WERR_NOT_SUPPORTED;
1604 /****************************************************************
1605 ****************************************************************/
1607 NTSTATUS _netr_DatabaseDeltas(struct pipes_struct *p,
1608 struct netr_DatabaseDeltas *r)
1610 p->rng_fault_state = true;
1611 return NT_STATUS_NOT_IMPLEMENTED;
1614 /****************************************************************
1615 ****************************************************************/
1617 NTSTATUS _netr_DatabaseSync(struct pipes_struct *p,
1618 struct netr_DatabaseSync *r)
1620 p->rng_fault_state = true;
1621 return NT_STATUS_NOT_IMPLEMENTED;
1624 /****************************************************************
1625 ****************************************************************/
1627 NTSTATUS _netr_AccountDeltas(struct pipes_struct *p,
1628 struct netr_AccountDeltas *r)
1630 p->rng_fault_state = true;
1631 return NT_STATUS_NOT_IMPLEMENTED;
1634 /****************************************************************
1635 ****************************************************************/
1637 NTSTATUS _netr_AccountSync(struct pipes_struct *p,
1638 struct netr_AccountSync *r)
1640 p->rng_fault_state = true;
1641 return NT_STATUS_NOT_IMPLEMENTED;
1644 /****************************************************************
1645 ****************************************************************/
1647 static bool wb_getdcname(TALLOC_CTX *mem_ctx,
1648 const char *domain,
1649 const char **dcname,
1650 uint32_t flags,
1651 WERROR *werr)
1653 wbcErr result;
1654 struct wbcDomainControllerInfo *dc_info = NULL;
1656 result = wbcLookupDomainController(domain,
1657 flags,
1658 &dc_info);
1659 switch (result) {
1660 case WBC_ERR_SUCCESS:
1661 break;
1662 case WBC_ERR_WINBIND_NOT_AVAILABLE:
1663 return false;
1664 case WBC_ERR_DOMAIN_NOT_FOUND:
1665 *werr = WERR_NO_SUCH_DOMAIN;
1666 return true;
1667 default:
1668 *werr = WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1669 return true;
1672 *dcname = talloc_strdup(mem_ctx, dc_info->dc_name);
1673 wbcFreeMemory(dc_info);
1674 if (!*dcname) {
1675 *werr = WERR_NOMEM;
1676 return false;
1679 *werr = WERR_OK;
1681 return true;
1684 /****************************************************************
1685 _netr_GetDcName
1686 ****************************************************************/
1688 WERROR _netr_GetDcName(struct pipes_struct *p,
1689 struct netr_GetDcName *r)
1691 NTSTATUS status;
1692 WERROR werr;
1693 uint32_t flags;
1694 struct netr_DsRGetDCNameInfo *info;
1695 bool ret;
1697 ret = wb_getdcname(p->mem_ctx,
1698 r->in.domainname,
1699 r->out.dcname,
1700 WBC_LOOKUP_DC_IS_FLAT_NAME |
1701 WBC_LOOKUP_DC_RETURN_FLAT_NAME |
1702 WBC_LOOKUP_DC_PDC_REQUIRED,
1703 &werr);
1704 if (ret == true) {
1705 return werr;
1708 flags = DS_PDC_REQUIRED | DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1710 status = dsgetdcname(p->mem_ctx,
1711 p->msg_ctx,
1712 r->in.domainname,
1713 NULL,
1714 NULL,
1715 flags,
1716 &info);
1717 if (!NT_STATUS_IS_OK(status)) {
1718 return ntstatus_to_werror(status);
1721 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1722 talloc_free(info);
1723 if (!*r->out.dcname) {
1724 return WERR_NOMEM;
1727 return WERR_OK;
1730 /****************************************************************
1731 _netr_GetAnyDCName
1732 ****************************************************************/
1734 WERROR _netr_GetAnyDCName(struct pipes_struct *p,
1735 struct netr_GetAnyDCName *r)
1737 NTSTATUS status;
1738 WERROR werr;
1739 uint32_t flags;
1740 struct netr_DsRGetDCNameInfo *info;
1741 bool ret;
1743 ret = wb_getdcname(p->mem_ctx,
1744 r->in.domainname,
1745 r->out.dcname,
1746 WBC_LOOKUP_DC_IS_FLAT_NAME |
1747 WBC_LOOKUP_DC_RETURN_FLAT_NAME,
1748 &werr);
1749 if (ret == true) {
1750 return werr;
1753 flags = DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1755 status = dsgetdcname(p->mem_ctx,
1756 p->msg_ctx,
1757 r->in.domainname,
1758 NULL,
1759 NULL,
1760 flags,
1761 &info);
1762 if (!NT_STATUS_IS_OK(status)) {
1763 return ntstatus_to_werror(status);
1766 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1767 talloc_free(info);
1768 if (!*r->out.dcname) {
1769 return WERR_NOMEM;
1772 return WERR_OK;
1775 /****************************************************************
1776 ****************************************************************/
1778 NTSTATUS _netr_DatabaseSync2(struct pipes_struct *p,
1779 struct netr_DatabaseSync2 *r)
1781 p->rng_fault_state = true;
1782 return NT_STATUS_NOT_IMPLEMENTED;
1785 /****************************************************************
1786 ****************************************************************/
1788 NTSTATUS _netr_DatabaseRedo(struct pipes_struct *p,
1789 struct netr_DatabaseRedo *r)
1791 p->rng_fault_state = true;
1792 return NT_STATUS_NOT_IMPLEMENTED;
1795 /****************************************************************
1796 ****************************************************************/
1798 WERROR _netr_DsRGetDCName(struct pipes_struct *p,
1799 struct netr_DsRGetDCName *r)
1801 p->rng_fault_state = true;
1802 return WERR_NOT_SUPPORTED;
1805 /****************************************************************
1806 ****************************************************************/
1808 NTSTATUS _netr_LogonGetCapabilities(struct pipes_struct *p,
1809 struct netr_LogonGetCapabilities *r)
1811 return NT_STATUS_NOT_IMPLEMENTED;
1814 /****************************************************************
1815 ****************************************************************/
1817 WERROR _netr_NETRLOGONSETSERVICEBITS(struct pipes_struct *p,
1818 struct netr_NETRLOGONSETSERVICEBITS *r)
1820 p->rng_fault_state = true;
1821 return WERR_NOT_SUPPORTED;
1824 /****************************************************************
1825 ****************************************************************/
1827 WERROR _netr_LogonGetTrustRid(struct pipes_struct *p,
1828 struct netr_LogonGetTrustRid *r)
1830 p->rng_fault_state = true;
1831 return WERR_NOT_SUPPORTED;
1834 /****************************************************************
1835 ****************************************************************/
1837 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(struct pipes_struct *p,
1838 struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
1840 p->rng_fault_state = true;
1841 return WERR_NOT_SUPPORTED;
1844 /****************************************************************
1845 ****************************************************************/
1847 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(struct pipes_struct *p,
1848 struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
1850 p->rng_fault_state = true;
1851 return WERR_NOT_SUPPORTED;
1854 /****************************************************************
1855 ****************************************************************/
1857 WERROR _netr_DsRGetDCNameEx(struct pipes_struct *p,
1858 struct netr_DsRGetDCNameEx *r)
1860 p->rng_fault_state = true;
1861 return WERR_NOT_SUPPORTED;
1864 /****************************************************************
1865 ****************************************************************/
1867 WERROR _netr_DsRGetSiteName(struct pipes_struct *p,
1868 struct netr_DsRGetSiteName *r)
1870 p->rng_fault_state = true;
1871 return WERR_NOT_SUPPORTED;
1874 /****************************************************************
1875 ****************************************************************/
1877 NTSTATUS _netr_LogonGetDomainInfo(struct pipes_struct *p,
1878 struct netr_LogonGetDomainInfo *r)
1880 p->rng_fault_state = true;
1881 return NT_STATUS_NOT_IMPLEMENTED;
1884 /****************************************************************
1885 ****************************************************************/
1887 WERROR _netr_ServerPasswordGet(struct pipes_struct *p,
1888 struct netr_ServerPasswordGet *r)
1890 p->rng_fault_state = true;
1891 return WERR_NOT_SUPPORTED;
1894 /****************************************************************
1895 ****************************************************************/
1897 WERROR _netr_NETRLOGONSENDTOSAM(struct pipes_struct *p,
1898 struct netr_NETRLOGONSENDTOSAM *r)
1900 p->rng_fault_state = true;
1901 return WERR_NOT_SUPPORTED;
1904 /****************************************************************
1905 ****************************************************************/
1907 WERROR _netr_DsRAddressToSitenamesW(struct pipes_struct *p,
1908 struct netr_DsRAddressToSitenamesW *r)
1910 p->rng_fault_state = true;
1911 return WERR_NOT_SUPPORTED;
1914 /****************************************************************
1915 ****************************************************************/
1917 WERROR _netr_DsRGetDCNameEx2(struct pipes_struct *p,
1918 struct netr_DsRGetDCNameEx2 *r)
1920 p->rng_fault_state = true;
1921 return WERR_NOT_SUPPORTED;
1924 /****************************************************************
1925 ****************************************************************/
1927 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(struct pipes_struct *p,
1928 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
1930 p->rng_fault_state = true;
1931 return WERR_NOT_SUPPORTED;
1934 /****************************************************************
1935 ****************************************************************/
1937 WERROR _netr_NetrEnumerateTrustedDomainsEx(struct pipes_struct *p,
1938 struct netr_NetrEnumerateTrustedDomainsEx *r)
1940 p->rng_fault_state = true;
1941 return WERR_NOT_SUPPORTED;
1944 /****************************************************************
1945 ****************************************************************/
1947 WERROR _netr_DsRAddressToSitenamesExW(struct pipes_struct *p,
1948 struct netr_DsRAddressToSitenamesExW *r)
1950 p->rng_fault_state = true;
1951 return WERR_NOT_SUPPORTED;
1954 /****************************************************************
1955 ****************************************************************/
1957 WERROR _netr_DsrGetDcSiteCoverageW(struct pipes_struct *p,
1958 struct netr_DsrGetDcSiteCoverageW *r)
1960 p->rng_fault_state = true;
1961 return WERR_NOT_SUPPORTED;
1964 /****************************************************************
1965 ****************************************************************/
1967 WERROR _netr_DsrEnumerateDomainTrusts(struct pipes_struct *p,
1968 struct netr_DsrEnumerateDomainTrusts *r)
1970 p->rng_fault_state = true;
1971 return WERR_NOT_SUPPORTED;
1974 /****************************************************************
1975 ****************************************************************/
1977 WERROR _netr_DsrDeregisterDNSHostRecords(struct pipes_struct *p,
1978 struct netr_DsrDeregisterDNSHostRecords *r)
1980 p->rng_fault_state = true;
1981 return WERR_NOT_SUPPORTED;
1984 /****************************************************************
1985 ****************************************************************/
1987 NTSTATUS _netr_ServerTrustPasswordsGet(struct pipes_struct *p,
1988 struct netr_ServerTrustPasswordsGet *r)
1990 p->rng_fault_state = true;
1991 return NT_STATUS_NOT_IMPLEMENTED;
1994 /****************************************************************
1995 ****************************************************************/
1997 WERROR _netr_DsRGetForestTrustInformation(struct pipes_struct *p,
1998 struct netr_DsRGetForestTrustInformation *r)
2000 p->rng_fault_state = true;
2001 return WERR_NOT_SUPPORTED;
2004 /****************************************************************
2005 ****************************************************************/
2007 NTSTATUS _netr_GetForestTrustInformation(struct pipes_struct *p,
2008 struct netr_GetForestTrustInformation *r)
2010 p->rng_fault_state = true;
2011 return NT_STATUS_NOT_IMPLEMENTED;
2014 /****************************************************************
2015 ****************************************************************/
2017 NTSTATUS _netr_ServerGetTrustInfo(struct pipes_struct *p,
2018 struct netr_ServerGetTrustInfo *r)
2020 p->rng_fault_state = true;
2021 return NT_STATUS_NOT_IMPLEMENTED;
2024 /****************************************************************
2025 ****************************************************************/
2027 NTSTATUS _netr_Unused47(struct pipes_struct *p,
2028 struct netr_Unused47 *r)
2030 p->rng_fault_state = true;
2031 return NT_STATUS_NOT_IMPLEMENTED;
2034 /****************************************************************
2035 ****************************************************************/
2037 NTSTATUS _netr_DsrUpdateReadOnlyServerDnsRecords(struct pipes_struct *p,
2038 struct netr_DsrUpdateReadOnlyServerDnsRecords *r)
2040 p->rng_fault_state = true;
2041 return NT_STATUS_NOT_IMPLEMENTED;