s3: use monotonic clock for time deltas in namequery functions
[Samba/gebeck_regimport.git] / source3 / rpc_server / srv_netlog_nt.c
blobd0f9dc9dba155cb60b3c629cf6d99c8c9246006f
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"
39 extern userdom_struct current_user_info;
41 #undef DBGC_CLASS
42 #define DBGC_CLASS DBGC_RPC_SRV
44 struct netlogon_server_pipe_state {
45 struct netr_Credential client_challenge;
46 struct netr_Credential server_challenge;
49 /*************************************************************************
50 _netr_LogonControl
51 *************************************************************************/
53 WERROR _netr_LogonControl(struct pipes_struct *p,
54 struct netr_LogonControl *r)
56 struct netr_LogonControl2Ex l;
58 switch (r->in.level) {
59 case 1:
60 break;
61 case 2:
62 return WERR_NOT_SUPPORTED;
63 default:
64 return WERR_UNKNOWN_LEVEL;
67 l.in.logon_server = r->in.logon_server;
68 l.in.function_code = r->in.function_code;
69 l.in.level = r->in.level;
70 l.in.data = NULL;
71 l.out.query = r->out.query;
73 return _netr_LogonControl2Ex(p, &l);
76 /****************************************************************************
77 Send a message to smbd to do a sam synchronisation
78 **************************************************************************/
80 static void send_sync_message(struct messaging_context *msg_ctx)
82 DEBUG(3, ("sending sam synchronisation message\n"));
83 message_send_all(msg_ctx, MSG_SMB_SAM_SYNC, NULL, 0, NULL);
86 /*************************************************************************
87 _netr_LogonControl2
88 *************************************************************************/
90 WERROR _netr_LogonControl2(struct pipes_struct *p,
91 struct netr_LogonControl2 *r)
93 struct netr_LogonControl2Ex l;
95 l.in.logon_server = r->in.logon_server;
96 l.in.function_code = r->in.function_code;
97 l.in.level = r->in.level;
98 l.in.data = r->in.data;
99 l.out.query = r->out.query;
101 return _netr_LogonControl2Ex(p, &l);
104 /*************************************************************************
105 *************************************************************************/
107 static bool wb_change_trust_creds(const char *domain, WERROR *tc_status)
109 wbcErr result;
110 struct wbcAuthErrorInfo *error = NULL;
112 result = wbcChangeTrustCredentials(domain, &error);
113 switch (result) {
114 case WBC_ERR_WINBIND_NOT_AVAILABLE:
115 return false;
116 case WBC_ERR_DOMAIN_NOT_FOUND:
117 *tc_status = WERR_NO_SUCH_DOMAIN;
118 return true;
119 case WBC_ERR_SUCCESS:
120 *tc_status = WERR_OK;
121 return true;
122 default:
123 break;
126 if (error && error->nt_status != 0) {
127 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
128 } else {
129 *tc_status = WERR_TRUST_FAILURE;
131 wbcFreeMemory(error);
132 return true;
135 /*************************************************************************
136 *************************************************************************/
138 static bool wb_check_trust_creds(const char *domain, WERROR *tc_status)
140 wbcErr result;
141 struct wbcAuthErrorInfo *error = NULL;
143 result = wbcCheckTrustCredentials(domain, &error);
144 switch (result) {
145 case WBC_ERR_WINBIND_NOT_AVAILABLE:
146 return false;
147 case WBC_ERR_DOMAIN_NOT_FOUND:
148 *tc_status = WERR_NO_SUCH_DOMAIN;
149 return true;
150 case WBC_ERR_SUCCESS:
151 *tc_status = WERR_OK;
152 return true;
153 default:
154 break;
157 if (error && error->nt_status != 0) {
158 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
159 } else {
160 *tc_status = WERR_TRUST_FAILURE;
162 wbcFreeMemory(error);
163 return true;
166 /****************************************************************
167 _netr_LogonControl2Ex
168 ****************************************************************/
170 WERROR _netr_LogonControl2Ex(struct pipes_struct *p,
171 struct netr_LogonControl2Ex *r)
173 uint32_t flags = 0x0;
174 WERROR pdc_connection_status = WERR_OK;
175 uint32_t logon_attempts = 0x0;
176 WERROR tc_status;
177 fstring dc_name2;
178 const char *dc_name = NULL;
179 struct sockaddr_storage dc_ss;
180 const char *domain = NULL;
181 struct netr_NETLOGON_INFO_1 *info1;
182 struct netr_NETLOGON_INFO_2 *info2;
183 struct netr_NETLOGON_INFO_3 *info3;
184 struct netr_NETLOGON_INFO_4 *info4;
185 const char *fn;
186 uint32_t acct_ctrl;
188 switch (p->opnum) {
189 case NDR_NETR_LOGONCONTROL:
190 fn = "_netr_LogonControl";
191 break;
192 case NDR_NETR_LOGONCONTROL2:
193 fn = "_netr_LogonControl2";
194 break;
195 case NDR_NETR_LOGONCONTROL2EX:
196 fn = "_netr_LogonControl2Ex";
197 break;
198 default:
199 return WERR_INVALID_PARAM;
202 acct_ctrl = p->server_info->info3->base.acct_flags;
204 switch (r->in.function_code) {
205 case NETLOGON_CONTROL_TC_VERIFY:
206 case NETLOGON_CONTROL_CHANGE_PASSWORD:
207 case NETLOGON_CONTROL_REDISCOVER:
208 if ((geteuid() != sec_initial_uid()) &&
209 !nt_token_check_domain_rid(p->server_info->ptok, DOMAIN_RID_ADMINS) &&
210 !nt_token_check_sid(&global_sid_Builtin_Administrators, p->server_info->ptok) &&
211 !(acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST))) {
212 return WERR_ACCESS_DENIED;
214 break;
215 default:
216 break;
219 tc_status = WERR_NO_SUCH_DOMAIN;
221 switch (r->in.function_code) {
222 case NETLOGON_CONTROL_QUERY:
223 tc_status = WERR_OK;
224 break;
225 case NETLOGON_CONTROL_REPLICATE:
226 case NETLOGON_CONTROL_SYNCHRONIZE:
227 case NETLOGON_CONTROL_PDC_REPLICATE:
228 case NETLOGON_CONTROL_BACKUP_CHANGE_LOG:
229 case NETLOGON_CONTROL_BREAKPOINT:
230 if (acct_ctrl & ACB_NORMAL) {
231 return WERR_NOT_SUPPORTED;
232 } else if (acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST)) {
233 return WERR_ACCESS_DENIED;
234 } else {
235 return WERR_ACCESS_DENIED;
237 case NETLOGON_CONTROL_TRUNCATE_LOG:
238 if (acct_ctrl & ACB_NORMAL) {
239 break;
240 } else if (acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST)) {
241 return WERR_ACCESS_DENIED;
242 } else {
243 return WERR_ACCESS_DENIED;
246 case NETLOGON_CONTROL_TRANSPORT_NOTIFY:
247 case NETLOGON_CONTROL_FORCE_DNS_REG:
248 case NETLOGON_CONTROL_QUERY_DNS_REG:
249 return WERR_NOT_SUPPORTED;
250 case NETLOGON_CONTROL_FIND_USER:
251 if (!r->in.data || !r->in.data->user) {
252 return WERR_NOT_SUPPORTED;
254 break;
255 case NETLOGON_CONTROL_SET_DBFLAG:
256 if (!r->in.data) {
257 return WERR_NOT_SUPPORTED;
259 break;
260 case NETLOGON_CONTROL_TC_VERIFY:
261 if (!r->in.data || !r->in.data->domain) {
262 return WERR_NOT_SUPPORTED;
265 if (!wb_check_trust_creds(r->in.data->domain, &tc_status)) {
266 return WERR_NOT_SUPPORTED;
268 break;
269 case NETLOGON_CONTROL_TC_QUERY:
270 if (!r->in.data || !r->in.data->domain) {
271 return WERR_NOT_SUPPORTED;
274 domain = r->in.data->domain;
276 if (!is_trusted_domain(domain)) {
277 break;
280 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
281 tc_status = WERR_NO_LOGON_SERVERS;
282 break;
285 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
286 if (!dc_name) {
287 return WERR_NOMEM;
290 tc_status = WERR_OK;
292 break;
294 case NETLOGON_CONTROL_REDISCOVER:
295 if (!r->in.data || !r->in.data->domain) {
296 return WERR_NOT_SUPPORTED;
299 domain = r->in.data->domain;
301 if (!is_trusted_domain(domain)) {
302 break;
305 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
306 tc_status = WERR_NO_LOGON_SERVERS;
307 break;
310 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
311 if (!dc_name) {
312 return WERR_NOMEM;
315 tc_status = WERR_OK;
317 break;
319 case NETLOGON_CONTROL_CHANGE_PASSWORD:
320 if (!r->in.data || !r->in.data->domain) {
321 return WERR_NOT_SUPPORTED;
324 if (!wb_change_trust_creds(r->in.data->domain, &tc_status)) {
325 return WERR_NOT_SUPPORTED;
327 break;
329 default:
330 /* no idea what this should be */
331 DEBUG(0,("%s: unimplemented function level [%d]\n",
332 fn, r->in.function_code));
333 return WERR_UNKNOWN_LEVEL;
336 /* prepare the response */
338 switch (r->in.level) {
339 case 1:
340 info1 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_1);
341 W_ERROR_HAVE_NO_MEMORY(info1);
343 info1->flags = flags;
344 info1->pdc_connection_status = pdc_connection_status;
346 r->out.query->info1 = info1;
347 break;
348 case 2:
349 info2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_2);
350 W_ERROR_HAVE_NO_MEMORY(info2);
352 info2->flags = flags;
353 info2->pdc_connection_status = pdc_connection_status;
354 info2->trusted_dc_name = dc_name;
355 info2->tc_connection_status = tc_status;
357 r->out.query->info2 = info2;
358 break;
359 case 3:
360 info3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_3);
361 W_ERROR_HAVE_NO_MEMORY(info3);
363 info3->flags = flags;
364 info3->logon_attempts = logon_attempts;
366 r->out.query->info3 = info3;
367 break;
368 case 4:
369 info4 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_4);
370 W_ERROR_HAVE_NO_MEMORY(info4);
372 info4->trusted_dc_name = dc_name;
373 info4->trusted_domain_name = r->in.data->domain;
375 r->out.query->info4 = info4;
376 break;
377 default:
378 return WERR_UNKNOWN_LEVEL;
381 if (lp_server_role() == ROLE_DOMAIN_BDC) {
382 send_sync_message(p->msg_ctx);
385 return WERR_OK;
388 /*************************************************************************
389 _netr_NetrEnumerateTrustedDomains
390 *************************************************************************/
392 NTSTATUS _netr_NetrEnumerateTrustedDomains(struct pipes_struct *p,
393 struct netr_NetrEnumerateTrustedDomains *r)
395 NTSTATUS status;
396 DATA_BLOB blob;
397 int num_domains = 0;
398 const char **trusted_domains = NULL;
399 struct lsa_DomainList domain_list;
400 struct rpc_pipe_client *cli = NULL;
401 struct policy_handle pol;
402 uint32_t enum_ctx = 0;
403 int i;
404 uint32_t max_size = (uint32_t)-1;
406 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
408 status = rpc_pipe_open_internal(p->mem_ctx, &ndr_table_lsarpc.syntax_id,
409 p->server_info,
410 p->client_id,
411 p->msg_ctx,
412 &cli);
413 if (!NT_STATUS_IS_OK(status)) {
414 return status;
417 status = rpccli_lsa_open_policy2(cli, p->mem_ctx,
418 true,
419 LSA_POLICY_VIEW_LOCAL_INFORMATION,
420 &pol);
421 if (!NT_STATUS_IS_OK(status)) {
422 goto out;
425 do {
426 /* Lookup list of trusted domains */
428 status = rpccli_lsa_EnumTrustDom(cli, p->mem_ctx,
429 &pol,
430 &enum_ctx,
431 &domain_list,
432 max_size);
433 if (!NT_STATUS_IS_OK(status) &&
434 !NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES) &&
435 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
436 goto out;
439 for (i = 0; i < domain_list.count; i++) {
440 if (!add_string_to_array(p->mem_ctx, domain_list.domains[i].name.string,
441 &trusted_domains, &num_domains)) {
442 status = NT_STATUS_NO_MEMORY;
443 goto out;
446 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
448 if (num_domains > 0) {
449 /* multi sz terminate */
450 trusted_domains = talloc_realloc(p->mem_ctx, trusted_domains, const char *, num_domains + 1);
451 if (trusted_domains == NULL) {
452 status = NT_STATUS_NO_MEMORY;
453 goto out;
456 trusted_domains[num_domains] = NULL;
459 if (!push_reg_multi_sz(trusted_domains, &blob, trusted_domains)) {
460 TALLOC_FREE(trusted_domains);
461 status = NT_STATUS_NO_MEMORY;
462 goto out;
465 r->out.trusted_domains_blob->data = blob.data;
466 r->out.trusted_domains_blob->length = blob.length;
468 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
470 status = NT_STATUS_OK;
472 out:
473 if (cli && is_valid_policy_hnd(&pol)) {
474 rpccli_lsa_Close(cli, p->mem_ctx, &pol);
477 return status;
480 /*************************************************************************
481 *************************************************************************/
483 static NTSTATUS samr_find_machine_account(TALLOC_CTX *mem_ctx,
484 struct rpc_pipe_client *cli,
485 const char *account_name,
486 uint32_t access_mask,
487 struct dom_sid2 **domain_sid_p,
488 uint32_t *user_rid_p,
489 struct policy_handle *user_handle)
491 NTSTATUS status;
492 struct policy_handle connect_handle, domain_handle;
493 struct lsa_String domain_name;
494 struct dom_sid2 *domain_sid;
495 struct lsa_String names;
496 struct samr_Ids rids;
497 struct samr_Ids types;
498 uint32_t rid;
500 status = rpccli_samr_Connect2(cli, mem_ctx,
501 global_myname(),
502 SAMR_ACCESS_CONNECT_TO_SERVER |
503 SAMR_ACCESS_ENUM_DOMAINS |
504 SAMR_ACCESS_LOOKUP_DOMAIN,
505 &connect_handle);
506 if (!NT_STATUS_IS_OK(status)) {
507 goto out;
510 init_lsa_String(&domain_name, get_global_sam_name());
512 status = rpccli_samr_LookupDomain(cli, mem_ctx,
513 &connect_handle,
514 &domain_name,
515 &domain_sid);
516 if (!NT_STATUS_IS_OK(status)) {
517 goto out;
520 status = rpccli_samr_OpenDomain(cli, mem_ctx,
521 &connect_handle,
522 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
523 domain_sid,
524 &domain_handle);
525 if (!NT_STATUS_IS_OK(status)) {
526 goto out;
529 init_lsa_String(&names, account_name);
531 status = rpccli_samr_LookupNames(cli, mem_ctx,
532 &domain_handle,
534 &names,
535 &rids,
536 &types);
537 if (!NT_STATUS_IS_OK(status)) {
538 goto out;
541 if (rids.count != 1) {
542 status = NT_STATUS_NO_SUCH_USER;
543 goto out;
545 if (rids.count != types.count) {
546 status = NT_STATUS_INVALID_PARAMETER;
547 goto out;
549 if (types.ids[0] != SID_NAME_USER) {
550 status = NT_STATUS_NO_SUCH_USER;
551 goto out;
554 rid = rids.ids[0];
556 status = rpccli_samr_OpenUser(cli, mem_ctx,
557 &domain_handle,
558 access_mask,
559 rid,
560 user_handle);
561 if (!NT_STATUS_IS_OK(status)) {
562 goto out;
565 if (user_rid_p) {
566 *user_rid_p = rid;
569 if (domain_sid_p) {
570 *domain_sid_p = domain_sid;
573 out:
574 if (cli && is_valid_policy_hnd(&domain_handle)) {
575 rpccli_samr_Close(cli, mem_ctx, &domain_handle);
577 if (cli && is_valid_policy_hnd(&connect_handle)) {
578 rpccli_samr_Close(cli, mem_ctx, &connect_handle);
581 return status;
584 /******************************************************************
585 gets a machine password entry. checks access rights of the host.
586 ******************************************************************/
588 static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
589 enum netr_SchannelType sec_chan_type,
590 struct dom_sid *sid,
591 struct messaging_context *msg_ctx)
593 NTSTATUS status;
594 TALLOC_CTX *mem_ctx;
595 struct rpc_pipe_client *cli = NULL;
596 struct policy_handle user_handle;
597 uint32_t user_rid;
598 struct dom_sid *domain_sid;
599 uint32_t acct_ctrl;
600 union samr_UserInfo *info;
601 struct auth_serversupplied_info *server_info;
602 #if 0
605 * Currently this code is redundent as we already have a filter
606 * by hostname list. What this code really needs to do is to
607 * get a hosts allowed/hosts denied list from the SAM database
608 * on a per user basis, and make the access decision there.
609 * I will leave this code here for now as a reminder to implement
610 * this at a later date. JRA.
613 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
614 p->client_id.name,
615 p->client_id.addr)) {
616 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
617 return False;
619 #endif /* 0 */
621 mem_ctx = talloc_new(talloc_tos());
622 if (mem_ctx == NULL) {
623 status = NT_STATUS_NO_MEMORY;
624 goto out;
627 status = make_server_info_system(mem_ctx, &server_info);
628 if (!NT_STATUS_IS_OK(status)) {
629 goto out;
632 ZERO_STRUCT(user_handle);
634 status = rpc_pipe_open_internal(mem_ctx, &ndr_table_samr.syntax_id,
635 server_info, NULL, msg_ctx,
636 &cli);
637 if (!NT_STATUS_IS_OK(status)) {
638 goto out;
641 become_root();
642 status = samr_find_machine_account(mem_ctx, cli, mach_acct,
643 SEC_FLAG_MAXIMUM_ALLOWED,
644 &domain_sid, &user_rid,
645 &user_handle);
646 unbecome_root();
647 if (!NT_STATUS_IS_OK(status)) {
648 goto out;
651 status = rpccli_samr_QueryUserInfo2(cli, mem_ctx,
652 &user_handle,
653 UserControlInformation,
654 &info);
655 if (!NT_STATUS_IS_OK(status)) {
656 goto out;
659 acct_ctrl = info->info16.acct_flags;
661 if (acct_ctrl & ACB_DISABLED) {
662 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
663 status = NT_STATUS_ACCOUNT_DISABLED;
664 goto out;
667 if (!(acct_ctrl & ACB_SVRTRUST) &&
668 !(acct_ctrl & ACB_WSTRUST) &&
669 !(acct_ctrl & ACB_DOMTRUST))
671 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
672 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
673 goto out;
676 switch (sec_chan_type) {
677 case SEC_CHAN_BDC:
678 if (!(acct_ctrl & ACB_SVRTRUST)) {
679 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
680 "but not a server trust account\n", mach_acct));
681 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
682 goto out;
684 break;
685 case SEC_CHAN_WKSTA:
686 if (!(acct_ctrl & ACB_WSTRUST)) {
687 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
688 "but not a workstation trust account\n", mach_acct));
689 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
690 goto out;
692 break;
693 case SEC_CHAN_DOMAIN:
694 if (!(acct_ctrl & ACB_DOMTRUST)) {
695 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
696 "but not a interdomain trust account\n", mach_acct));
697 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
698 goto out;
700 break;
701 default:
702 break;
705 become_root();
706 status = rpccli_samr_QueryUserInfo2(cli, mem_ctx,
707 &user_handle,
708 UserInternal1Information,
709 &info);
710 unbecome_root();
711 if (!NT_STATUS_IS_OK(status)) {
712 goto out;
714 if (info->info18.nt_pwd_active == 0) {
715 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
716 status = NT_STATUS_LOGON_FAILURE;
717 goto out;
720 /* samr gives out nthash unencrypted (!) */
721 memcpy(md4pw->hash, info->info18.nt_pwd.hash, 16);
723 sid_compose(sid, domain_sid, user_rid);
725 out:
726 if (cli && is_valid_policy_hnd(&user_handle)) {
727 rpccli_samr_Close(cli, mem_ctx, &user_handle);
730 talloc_free(mem_ctx);
732 return status;
735 /*************************************************************************
736 _netr_ServerReqChallenge
737 *************************************************************************/
739 NTSTATUS _netr_ServerReqChallenge(struct pipes_struct *p,
740 struct netr_ServerReqChallenge *r)
742 struct netlogon_server_pipe_state *pipe_state =
743 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
745 if (pipe_state) {
746 DEBUG(10,("_netr_ServerReqChallenge: new challenge requested. Clearing old state.\n"));
747 talloc_free(pipe_state);
748 p->private_data = NULL;
751 pipe_state = talloc(p, struct netlogon_server_pipe_state);
752 NT_STATUS_HAVE_NO_MEMORY(pipe_state);
754 pipe_state->client_challenge = *r->in.credentials;
756 generate_random_buffer(pipe_state->server_challenge.data,
757 sizeof(pipe_state->server_challenge.data));
759 *r->out.return_credentials = pipe_state->server_challenge;
761 p->private_data = pipe_state;
763 return NT_STATUS_OK;
766 /*************************************************************************
767 _netr_ServerAuthenticate
768 Create the initial credentials.
769 *************************************************************************/
771 NTSTATUS _netr_ServerAuthenticate(struct pipes_struct *p,
772 struct netr_ServerAuthenticate *r)
774 struct netr_ServerAuthenticate3 a;
775 uint32_t negotiate_flags = 0;
776 uint32_t rid;
778 a.in.server_name = r->in.server_name;
779 a.in.account_name = r->in.account_name;
780 a.in.secure_channel_type = r->in.secure_channel_type;
781 a.in.computer_name = r->in.computer_name;
782 a.in.credentials = r->in.credentials;
783 a.in.negotiate_flags = &negotiate_flags;
785 a.out.return_credentials = r->out.return_credentials;
786 a.out.rid = &rid;
787 a.out.negotiate_flags = &negotiate_flags;
789 return _netr_ServerAuthenticate3(p, &a);
793 /*************************************************************************
794 _netr_ServerAuthenticate3
795 *************************************************************************/
797 NTSTATUS _netr_ServerAuthenticate3(struct pipes_struct *p,
798 struct netr_ServerAuthenticate3 *r)
800 NTSTATUS status;
801 uint32_t srv_flgs;
802 /* r->in.negotiate_flags is an aliased pointer to r->out.negotiate_flags,
803 * so use a copy to avoid destroying the client values. */
804 uint32_t in_neg_flags = *r->in.negotiate_flags;
805 const char *fn;
806 struct dom_sid sid;
807 struct samr_Password mach_pwd;
808 struct netlogon_creds_CredentialState *creds;
809 struct netlogon_server_pipe_state *pipe_state =
810 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
812 /* According to Microsoft (see bugid #6099)
813 * Windows 7 looks at the negotiate_flags
814 * returned in this structure *even if the
815 * call fails with access denied* ! So in order
816 * to allow Win7 to connect to a Samba NT style
817 * PDC we set the flags before we know if it's
818 * an error or not.
821 /* 0x000001ff */
822 srv_flgs = NETLOGON_NEG_ACCOUNT_LOCKOUT |
823 NETLOGON_NEG_PERSISTENT_SAMREPL |
824 NETLOGON_NEG_ARCFOUR |
825 NETLOGON_NEG_PROMOTION_COUNT |
826 NETLOGON_NEG_CHANGELOG_BDC |
827 NETLOGON_NEG_FULL_SYNC_REPL |
828 NETLOGON_NEG_MULTIPLE_SIDS |
829 NETLOGON_NEG_REDO |
830 NETLOGON_NEG_PASSWORD_CHANGE_REFUSAL |
831 NETLOGON_NEG_PASSWORD_SET2;
833 /* Ensure we support strong (128-bit) keys. */
834 if (in_neg_flags & NETLOGON_NEG_STRONG_KEYS) {
835 srv_flgs |= NETLOGON_NEG_STRONG_KEYS;
838 if (lp_server_schannel() != false) {
839 srv_flgs |= NETLOGON_NEG_SCHANNEL;
842 switch (p->opnum) {
843 case NDR_NETR_SERVERAUTHENTICATE:
844 fn = "_netr_ServerAuthenticate";
845 break;
846 case NDR_NETR_SERVERAUTHENTICATE2:
847 fn = "_netr_ServerAuthenticate2";
848 break;
849 case NDR_NETR_SERVERAUTHENTICATE3:
850 fn = "_netr_ServerAuthenticate3";
851 break;
852 default:
853 return NT_STATUS_INTERNAL_ERROR;
856 /* We use this as the key to store the creds: */
857 /* r->in.computer_name */
859 if (!pipe_state) {
860 DEBUG(0,("%s: no challenge sent to client %s\n", fn,
861 r->in.computer_name));
862 status = NT_STATUS_ACCESS_DENIED;
863 goto out;
866 if ( (lp_server_schannel() == true) &&
867 ((in_neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
869 /* schannel must be used, but client did not offer it. */
870 DEBUG(0,("%s: schannel required but client failed "
871 "to offer it. Client was %s\n",
872 fn, r->in.account_name));
873 status = NT_STATUS_ACCESS_DENIED;
874 goto out;
877 status = get_md4pw(&mach_pwd,
878 r->in.account_name,
879 r->in.secure_channel_type,
880 &sid, p->msg_ctx);
881 if (!NT_STATUS_IS_OK(status)) {
882 DEBUG(0,("%s: failed to get machine password for "
883 "account %s: %s\n",
884 fn, r->in.account_name, nt_errstr(status) ));
885 /* always return NT_STATUS_ACCESS_DENIED */
886 status = NT_STATUS_ACCESS_DENIED;
887 goto out;
890 /* From the client / server challenges and md4 password, generate sess key */
891 /* Check client credentials are valid. */
892 creds = netlogon_creds_server_init(p->mem_ctx,
893 r->in.account_name,
894 r->in.computer_name,
895 r->in.secure_channel_type,
896 &pipe_state->client_challenge,
897 &pipe_state->server_challenge,
898 &mach_pwd,
899 r->in.credentials,
900 r->out.return_credentials,
901 *r->in.negotiate_flags);
902 if (!creds) {
903 DEBUG(0,("%s: netlogon_creds_server_check failed. Rejecting auth "
904 "request from client %s machine account %s\n",
905 fn, r->in.computer_name,
906 r->in.account_name));
907 status = NT_STATUS_ACCESS_DENIED;
908 goto out;
911 creds->sid = sid_dup_talloc(creds, &sid);
912 if (!creds->sid) {
913 status = NT_STATUS_NO_MEMORY;
914 goto out;
917 /* Store off the state so we can continue after client disconnect. */
918 become_root();
919 status = schannel_save_creds_state(p->mem_ctx, lp_private_dir(), creds);
920 unbecome_root();
922 if (!NT_STATUS_IS_OK(status)) {
923 goto out;
926 sid_peek_rid(&sid, r->out.rid);
928 status = NT_STATUS_OK;
930 out:
932 *r->out.negotiate_flags = srv_flgs;
933 return status;
936 /*************************************************************************
937 _netr_ServerAuthenticate2
938 *************************************************************************/
940 NTSTATUS _netr_ServerAuthenticate2(struct pipes_struct *p,
941 struct netr_ServerAuthenticate2 *r)
943 struct netr_ServerAuthenticate3 a;
944 uint32_t rid;
946 a.in.server_name = r->in.server_name;
947 a.in.account_name = r->in.account_name;
948 a.in.secure_channel_type = r->in.secure_channel_type;
949 a.in.computer_name = r->in.computer_name;
950 a.in.credentials = r->in.credentials;
951 a.in.negotiate_flags = r->in.negotiate_flags;
953 a.out.return_credentials = r->out.return_credentials;
954 a.out.rid = &rid;
955 a.out.negotiate_flags = r->out.negotiate_flags;
957 return _netr_ServerAuthenticate3(p, &a);
960 /*************************************************************************
961 * If schannel is required for this call test that it actually is available.
962 *************************************************************************/
963 static NTSTATUS schannel_check_required(struct pipe_auth_data *auth_info,
964 const char *computer_name,
965 bool integrity, bool privacy)
967 if (auth_info && auth_info->auth_type == DCERPC_AUTH_TYPE_SCHANNEL) {
968 if (!privacy && !integrity) {
969 return NT_STATUS_OK;
972 if ((!privacy && integrity) &&
973 auth_info->auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
974 return NT_STATUS_OK;
977 if ((privacy || integrity) &&
978 auth_info->auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
979 return NT_STATUS_OK;
983 /* test didn't pass */
984 DEBUG(0, ("schannel_check_required: [%s] is not using schannel\n",
985 computer_name));
987 return NT_STATUS_ACCESS_DENIED;
990 /*************************************************************************
991 *************************************************************************/
993 static NTSTATUS netr_creds_server_step_check(struct pipes_struct *p,
994 TALLOC_CTX *mem_ctx,
995 const char *computer_name,
996 struct netr_Authenticator *received_authenticator,
997 struct netr_Authenticator *return_authenticator,
998 struct netlogon_creds_CredentialState **creds_out)
1000 NTSTATUS status;
1001 bool schannel_global_required = (lp_server_schannel() == true) ? true:false;
1003 if (schannel_global_required) {
1004 status = schannel_check_required(&p->auth,
1005 computer_name,
1006 false, false);
1007 if (!NT_STATUS_IS_OK(status)) {
1008 return status;
1012 status = schannel_check_creds_state(mem_ctx, lp_private_dir(),
1013 computer_name, received_authenticator,
1014 return_authenticator, creds_out);
1016 return status;
1019 /*************************************************************************
1020 *************************************************************************/
1022 static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
1023 struct auth_serversupplied_info *server_info,
1024 struct messaging_context *msg_ctx,
1025 const char *account_name,
1026 struct samr_Password *nt_hash)
1028 NTSTATUS status;
1029 struct rpc_pipe_client *cli = NULL;
1030 struct policy_handle user_handle;
1031 uint32_t acct_ctrl;
1032 union samr_UserInfo *info;
1033 struct samr_UserInfo18 info18;
1034 DATA_BLOB in,out;
1036 ZERO_STRUCT(user_handle);
1038 status = rpc_pipe_open_internal(mem_ctx, &ndr_table_samr.syntax_id,
1039 server_info, NULL, msg_ctx,
1040 &cli);
1041 if (!NT_STATUS_IS_OK(status)) {
1042 goto out;
1045 status = samr_find_machine_account(mem_ctx, cli, account_name,
1046 SEC_FLAG_MAXIMUM_ALLOWED,
1047 NULL, NULL,
1048 &user_handle);
1049 if (!NT_STATUS_IS_OK(status)) {
1050 goto out;
1053 status = rpccli_samr_QueryUserInfo2(cli, mem_ctx,
1054 &user_handle,
1055 UserControlInformation,
1056 &info);
1057 if (!NT_STATUS_IS_OK(status)) {
1058 goto out;
1061 acct_ctrl = info->info16.acct_flags;
1063 if (!(acct_ctrl & ACB_WSTRUST ||
1064 acct_ctrl & ACB_SVRTRUST ||
1065 acct_ctrl & ACB_DOMTRUST)) {
1066 status = NT_STATUS_NO_SUCH_USER;
1067 goto out;
1070 if (acct_ctrl & ACB_DISABLED) {
1071 status = NT_STATUS_ACCOUNT_DISABLED;
1072 goto out;
1075 ZERO_STRUCT(info18);
1077 in = data_blob_const(nt_hash->hash, 16);
1078 out = data_blob_talloc_zero(mem_ctx, 16);
1079 sess_crypt_blob(&out, &in, &server_info->user_session_key, true);
1080 memcpy(info18.nt_pwd.hash, out.data, out.length);
1082 info18.nt_pwd_active = true;
1084 info->info18 = info18;
1086 status = rpccli_samr_SetUserInfo2(cli, mem_ctx,
1087 &user_handle,
1088 UserInternal1Information,
1089 info);
1090 if (!NT_STATUS_IS_OK(status)) {
1091 goto out;
1094 out:
1095 if (cli && is_valid_policy_hnd(&user_handle)) {
1096 rpccli_samr_Close(cli, mem_ctx, &user_handle);
1099 return status;
1102 /*************************************************************************
1103 _netr_ServerPasswordSet
1104 *************************************************************************/
1106 NTSTATUS _netr_ServerPasswordSet(struct pipes_struct *p,
1107 struct netr_ServerPasswordSet *r)
1109 NTSTATUS status = NT_STATUS_OK;
1110 int i;
1111 struct netlogon_creds_CredentialState *creds;
1113 DEBUG(5,("_netr_ServerPasswordSet: %d\n", __LINE__));
1115 become_root();
1116 status = netr_creds_server_step_check(p, p->mem_ctx,
1117 r->in.computer_name,
1118 r->in.credential,
1119 r->out.return_authenticator,
1120 &creds);
1121 unbecome_root();
1123 if (!NT_STATUS_IS_OK(status)) {
1124 DEBUG(2,("_netr_ServerPasswordSet: netlogon_creds_server_step failed. Rejecting auth "
1125 "request from client %s machine account %s\n",
1126 r->in.computer_name, creds->computer_name));
1127 TALLOC_FREE(creds);
1128 return status;
1131 DEBUG(3,("_netr_ServerPasswordSet: Server Password Set by remote machine:[%s] on account [%s]\n",
1132 r->in.computer_name, creds->computer_name));
1134 netlogon_creds_des_decrypt(creds, r->in.new_password);
1136 DEBUG(100,("_netr_ServerPasswordSet: new given value was :\n"));
1137 for(i = 0; i < sizeof(r->in.new_password->hash); i++)
1138 DEBUG(100,("%02X ", r->in.new_password->hash[i]));
1139 DEBUG(100,("\n"));
1141 status = netr_set_machine_account_password(p->mem_ctx,
1142 p->server_info,
1143 p->msg_ctx,
1144 creds->account_name,
1145 r->in.new_password);
1146 return status;
1149 /****************************************************************
1150 _netr_ServerPasswordSet2
1151 ****************************************************************/
1153 NTSTATUS _netr_ServerPasswordSet2(struct pipes_struct *p,
1154 struct netr_ServerPasswordSet2 *r)
1156 NTSTATUS status;
1157 struct netlogon_creds_CredentialState *creds;
1158 DATA_BLOB plaintext;
1159 struct samr_CryptPassword password_buf;
1160 struct samr_Password nt_hash;
1162 become_root();
1163 status = netr_creds_server_step_check(p, p->mem_ctx,
1164 r->in.computer_name,
1165 r->in.credential,
1166 r->out.return_authenticator,
1167 &creds);
1168 unbecome_root();
1170 if (!NT_STATUS_IS_OK(status)) {
1171 DEBUG(2,("_netr_ServerPasswordSet2: netlogon_creds_server_step "
1172 "failed. Rejecting auth request from client %s machine account %s\n",
1173 r->in.computer_name, creds->computer_name));
1174 TALLOC_FREE(creds);
1175 return status;
1178 memcpy(password_buf.data, r->in.new_password->data, 512);
1179 SIVAL(password_buf.data, 512, r->in.new_password->length);
1180 netlogon_creds_arcfour_crypt(creds, password_buf.data, 516);
1182 if (!extract_pw_from_buffer(p->mem_ctx, password_buf.data, &plaintext)) {
1183 return NT_STATUS_WRONG_PASSWORD;
1186 mdfour(nt_hash.hash, plaintext.data, plaintext.length);
1188 status = netr_set_machine_account_password(p->mem_ctx,
1189 p->server_info,
1190 p->msg_ctx,
1191 creds->account_name,
1192 &nt_hash);
1193 return status;
1196 /*************************************************************************
1197 _netr_LogonSamLogoff
1198 *************************************************************************/
1200 NTSTATUS _netr_LogonSamLogoff(struct pipes_struct *p,
1201 struct netr_LogonSamLogoff *r)
1203 NTSTATUS status;
1204 struct netlogon_creds_CredentialState *creds;
1206 become_root();
1207 status = netr_creds_server_step_check(p, p->mem_ctx,
1208 r->in.computer_name,
1209 r->in.credential,
1210 r->out.return_authenticator,
1211 &creds);
1212 unbecome_root();
1214 return status;
1217 /*************************************************************************
1218 _netr_LogonSamLogon_base
1219 *************************************************************************/
1221 static NTSTATUS _netr_LogonSamLogon_base(struct pipes_struct *p,
1222 struct netr_LogonSamLogonEx *r,
1223 struct netlogon_creds_CredentialState *creds)
1225 NTSTATUS status = NT_STATUS_OK;
1226 union netr_LogonLevel *logon = r->in.logon;
1227 const char *nt_username, *nt_domain, *nt_workstation;
1228 struct auth_usersupplied_info *user_info = NULL;
1229 struct auth_serversupplied_info *server_info = NULL;
1230 struct auth_context *auth_context = NULL;
1231 uint8_t pipe_session_key[16];
1232 bool process_creds = true;
1233 const char *fn;
1235 switch (p->opnum) {
1236 case NDR_NETR_LOGONSAMLOGON:
1237 process_creds = true;
1238 fn = "_netr_LogonSamLogon";
1239 break;
1240 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
1241 process_creds = true;
1242 fn = "_netr_LogonSamLogonWithFlags";
1243 break;
1244 case NDR_NETR_LOGONSAMLOGONEX:
1245 process_creds = false;
1246 fn = "_netr_LogonSamLogonEx";
1247 break;
1248 default:
1249 return NT_STATUS_INTERNAL_ERROR;
1252 *r->out.authoritative = true; /* authoritative response */
1254 switch (r->in.validation_level) {
1255 case 2:
1256 r->out.validation->sam2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo2);
1257 if (!r->out.validation->sam2) {
1258 return NT_STATUS_NO_MEMORY;
1260 break;
1261 case 3:
1262 r->out.validation->sam3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo3);
1263 if (!r->out.validation->sam3) {
1264 return NT_STATUS_NO_MEMORY;
1266 break;
1267 case 6:
1268 r->out.validation->sam6 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo6);
1269 if (!r->out.validation->sam6) {
1270 return NT_STATUS_NO_MEMORY;
1272 break;
1273 default:
1274 DEBUG(0,("%s: bad validation_level value %d.\n",
1275 fn, (int)r->in.validation_level));
1276 return NT_STATUS_INVALID_INFO_CLASS;
1279 switch (r->in.logon_level) {
1280 case NetlogonInteractiveInformation:
1281 case NetlogonServiceInformation:
1282 case NetlogonInteractiveTransitiveInformation:
1283 case NetlogonServiceTransitiveInformation:
1284 nt_username = logon->password->identity_info.account_name.string ?
1285 logon->password->identity_info.account_name.string : "";
1286 nt_domain = logon->password->identity_info.domain_name.string ?
1287 logon->password->identity_info.domain_name.string : "";
1288 nt_workstation = logon->password->identity_info.workstation.string ?
1289 logon->password->identity_info.workstation.string : "";
1291 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
1292 break;
1293 case NetlogonNetworkInformation:
1294 case NetlogonNetworkTransitiveInformation:
1295 nt_username = logon->network->identity_info.account_name.string ?
1296 logon->network->identity_info.account_name.string : "";
1297 nt_domain = logon->network->identity_info.domain_name.string ?
1298 logon->network->identity_info.domain_name.string : "";
1299 nt_workstation = logon->network->identity_info.workstation.string ?
1300 logon->network->identity_info.workstation.string : "";
1302 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
1303 break;
1304 default:
1305 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1306 return NT_STATUS_INVALID_INFO_CLASS;
1307 } /* end switch */
1309 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
1310 fstrcpy(current_user_info.smb_name, nt_username);
1311 sub_set_smb_name(nt_username);
1313 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
1314 r->in.validation_level, nt_username));
1316 status = NT_STATUS_OK;
1318 switch (r->in.logon_level) {
1319 case NetlogonNetworkInformation:
1320 case NetlogonNetworkTransitiveInformation:
1322 const char *wksname = nt_workstation;
1324 status = make_auth_context_fixed(&auth_context,
1325 logon->network->challenge);
1326 if (!NT_STATUS_IS_OK(status)) {
1327 return status;
1330 /* For a network logon, the workstation name comes in with two
1331 * backslashes in the front. Strip them if they are there. */
1333 if (*wksname == '\\') wksname++;
1334 if (*wksname == '\\') wksname++;
1336 /* Standard challenge/response authenticaion */
1337 if (!make_user_info_netlogon_network(&user_info,
1338 nt_username, nt_domain,
1339 wksname,
1340 logon->network->identity_info.parameter_control,
1341 logon->network->lm.data,
1342 logon->network->lm.length,
1343 logon->network->nt.data,
1344 logon->network->nt.length)) {
1345 status = NT_STATUS_NO_MEMORY;
1347 break;
1349 case NetlogonInteractiveInformation:
1350 case NetlogonServiceInformation:
1351 case NetlogonInteractiveTransitiveInformation:
1352 case NetlogonServiceTransitiveInformation:
1354 /* 'Interactive' authentication, supplies the password in its
1355 MD4 form, encrypted with the session key. We will convert
1356 this to challenge/response for the auth subsystem to chew
1357 on */
1359 uint8_t chal[8];
1361 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
1362 return status;
1365 auth_context->get_ntlm_challenge(auth_context, chal);
1367 if (!make_user_info_netlogon_interactive(&user_info,
1368 nt_username, nt_domain,
1369 nt_workstation,
1370 logon->password->identity_info.parameter_control,
1371 chal,
1372 logon->password->lmpassword.hash,
1373 logon->password->ntpassword.hash,
1374 creds->session_key)) {
1375 status = NT_STATUS_NO_MEMORY;
1377 break;
1379 default:
1380 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1381 return NT_STATUS_INVALID_INFO_CLASS;
1382 } /* end switch */
1384 if ( NT_STATUS_IS_OK(status) ) {
1385 status = auth_context->check_ntlm_password(auth_context,
1386 user_info, &server_info);
1389 TALLOC_FREE(auth_context);
1390 free_user_info(&user_info);
1392 DEBUG(5,("%s: check_password returned status %s\n",
1393 fn, nt_errstr(status)));
1395 /* Check account and password */
1397 if (!NT_STATUS_IS_OK(status)) {
1398 /* If we don't know what this domain is, we need to
1399 indicate that we are not authoritative. This
1400 allows the client to decide if it needs to try
1401 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
1402 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1403 && !strequal(nt_domain, get_global_sam_name())
1404 && !is_trusted_domain(nt_domain) )
1405 *r->out.authoritative = false; /* We are not authoritative */
1407 TALLOC_FREE(server_info);
1408 return status;
1411 if (server_info->guest) {
1412 /* We don't like guest domain logons... */
1413 DEBUG(5,("%s: Attempted domain logon as GUEST "
1414 "denied.\n", fn));
1415 TALLOC_FREE(server_info);
1416 return NT_STATUS_LOGON_FAILURE;
1419 /* This is the point at which, if the login was successful, that
1420 the SAM Local Security Authority should record that the user is
1421 logged in to the domain. */
1423 if (process_creds) {
1424 /* Get the pipe session key from the creds. */
1425 memcpy(pipe_session_key, creds->session_key, 16);
1426 } else {
1427 /* Get the pipe session key from the schannel. */
1428 if ((p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL)
1429 || (p->auth.a_u.schannel_auth == NULL)) {
1430 return NT_STATUS_INVALID_HANDLE;
1432 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->creds->session_key, 16);
1435 switch (r->in.validation_level) {
1436 case 2:
1437 status = serverinfo_to_SamInfo2(server_info, pipe_session_key, 16,
1438 r->out.validation->sam2);
1439 break;
1440 case 3:
1441 status = serverinfo_to_SamInfo3(server_info, pipe_session_key, 16,
1442 r->out.validation->sam3);
1443 break;
1444 case 6:
1445 status = serverinfo_to_SamInfo6(server_info, pipe_session_key, 16,
1446 r->out.validation->sam6);
1447 break;
1450 TALLOC_FREE(server_info);
1452 return status;
1455 /****************************************************************
1456 _netr_LogonSamLogonWithFlags
1457 ****************************************************************/
1459 NTSTATUS _netr_LogonSamLogonWithFlags(struct pipes_struct *p,
1460 struct netr_LogonSamLogonWithFlags *r)
1462 NTSTATUS status;
1463 struct netlogon_creds_CredentialState *creds;
1464 struct netr_LogonSamLogonEx r2;
1465 struct netr_Authenticator return_authenticator;
1467 become_root();
1468 status = netr_creds_server_step_check(p, p->mem_ctx,
1469 r->in.computer_name,
1470 r->in.credential,
1471 &return_authenticator,
1472 &creds);
1473 unbecome_root();
1474 if (!NT_STATUS_IS_OK(status)) {
1475 return status;
1478 r2.in.server_name = r->in.server_name;
1479 r2.in.computer_name = r->in.computer_name;
1480 r2.in.logon_level = r->in.logon_level;
1481 r2.in.logon = r->in.logon;
1482 r2.in.validation_level = r->in.validation_level;
1483 r2.in.flags = r->in.flags;
1484 r2.out.validation = r->out.validation;
1485 r2.out.authoritative = r->out.authoritative;
1486 r2.out.flags = r->out.flags;
1488 status = _netr_LogonSamLogon_base(p, &r2, creds);
1490 *r->out.return_authenticator = return_authenticator;
1492 return status;
1495 /*************************************************************************
1496 _netr_LogonSamLogon
1497 *************************************************************************/
1499 NTSTATUS _netr_LogonSamLogon(struct pipes_struct *p,
1500 struct netr_LogonSamLogon *r)
1502 NTSTATUS status;
1503 struct netr_LogonSamLogonWithFlags r2;
1504 uint32_t flags = 0;
1506 r2.in.server_name = r->in.server_name;
1507 r2.in.computer_name = r->in.computer_name;
1508 r2.in.credential = r->in.credential;
1509 r2.in.logon_level = r->in.logon_level;
1510 r2.in.logon = r->in.logon;
1511 r2.in.validation_level = r->in.validation_level;
1512 r2.in.return_authenticator = r->in.return_authenticator;
1513 r2.in.flags = &flags;
1514 r2.out.validation = r->out.validation;
1515 r2.out.authoritative = r->out.authoritative;
1516 r2.out.flags = &flags;
1517 r2.out.return_authenticator = r->out.return_authenticator;
1519 status = _netr_LogonSamLogonWithFlags(p, &r2);
1521 return status;
1524 /*************************************************************************
1525 _netr_LogonSamLogonEx
1526 - no credential chaining. Map into net sam logon.
1527 *************************************************************************/
1529 NTSTATUS _netr_LogonSamLogonEx(struct pipes_struct *p,
1530 struct netr_LogonSamLogonEx *r)
1532 NTSTATUS status;
1533 struct netlogon_creds_CredentialState *creds = NULL;
1535 become_root();
1536 status = schannel_get_creds_state(p->mem_ctx, lp_private_dir(),
1537 r->in.computer_name, &creds);
1538 unbecome_root();
1539 if (!NT_STATUS_IS_OK(status)) {
1540 return status;
1543 /* Only allow this if the pipe is protected. */
1544 if (p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
1545 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1546 get_remote_machine_name() ));
1547 return NT_STATUS_INVALID_PARAMETER;
1550 status = _netr_LogonSamLogon_base(p, r, creds);
1551 TALLOC_FREE(creds);
1553 return status;
1556 /*************************************************************************
1557 _ds_enum_dom_trusts
1558 *************************************************************************/
1559 #if 0 /* JERRY -- not correct */
1560 NTSTATUS _ds_enum_dom_trusts(struct pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1561 DS_R_ENUM_DOM_TRUSTS *r_u)
1563 NTSTATUS status = NT_STATUS_OK;
1565 /* TODO: According to MSDN, the can only be executed against a
1566 DC or domain member running Windows 2000 or later. Need
1567 to test against a standalone 2k server and see what it
1568 does. A windows 2000 DC includes its own domain in the
1569 list. --jerry */
1571 return status;
1573 #endif /* JERRY */
1576 /****************************************************************
1577 ****************************************************************/
1579 WERROR _netr_LogonUasLogon(struct pipes_struct *p,
1580 struct netr_LogonUasLogon *r)
1582 p->rng_fault_state = true;
1583 return WERR_NOT_SUPPORTED;
1586 /****************************************************************
1587 ****************************************************************/
1589 WERROR _netr_LogonUasLogoff(struct pipes_struct *p,
1590 struct netr_LogonUasLogoff *r)
1592 p->rng_fault_state = true;
1593 return WERR_NOT_SUPPORTED;
1596 /****************************************************************
1597 ****************************************************************/
1599 NTSTATUS _netr_DatabaseDeltas(struct pipes_struct *p,
1600 struct netr_DatabaseDeltas *r)
1602 p->rng_fault_state = true;
1603 return NT_STATUS_NOT_IMPLEMENTED;
1606 /****************************************************************
1607 ****************************************************************/
1609 NTSTATUS _netr_DatabaseSync(struct pipes_struct *p,
1610 struct netr_DatabaseSync *r)
1612 p->rng_fault_state = true;
1613 return NT_STATUS_NOT_IMPLEMENTED;
1616 /****************************************************************
1617 ****************************************************************/
1619 NTSTATUS _netr_AccountDeltas(struct pipes_struct *p,
1620 struct netr_AccountDeltas *r)
1622 p->rng_fault_state = true;
1623 return NT_STATUS_NOT_IMPLEMENTED;
1626 /****************************************************************
1627 ****************************************************************/
1629 NTSTATUS _netr_AccountSync(struct pipes_struct *p,
1630 struct netr_AccountSync *r)
1632 p->rng_fault_state = true;
1633 return NT_STATUS_NOT_IMPLEMENTED;
1636 /****************************************************************
1637 ****************************************************************/
1639 static bool wb_getdcname(TALLOC_CTX *mem_ctx,
1640 const char *domain,
1641 const char **dcname,
1642 uint32_t flags,
1643 WERROR *werr)
1645 wbcErr result;
1646 struct wbcDomainControllerInfo *dc_info = NULL;
1648 result = wbcLookupDomainController(domain,
1649 flags,
1650 &dc_info);
1651 switch (result) {
1652 case WBC_ERR_SUCCESS:
1653 break;
1654 case WBC_ERR_WINBIND_NOT_AVAILABLE:
1655 return false;
1656 case WBC_ERR_DOMAIN_NOT_FOUND:
1657 *werr = WERR_NO_SUCH_DOMAIN;
1658 return true;
1659 default:
1660 *werr = WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1661 return true;
1664 *dcname = talloc_strdup(mem_ctx, dc_info->dc_name);
1665 wbcFreeMemory(dc_info);
1666 if (!*dcname) {
1667 *werr = WERR_NOMEM;
1668 return false;
1671 *werr = WERR_OK;
1673 return true;
1676 /****************************************************************
1677 _netr_GetDcName
1678 ****************************************************************/
1680 WERROR _netr_GetDcName(struct pipes_struct *p,
1681 struct netr_GetDcName *r)
1683 NTSTATUS status;
1684 WERROR werr;
1685 uint32_t flags;
1686 struct netr_DsRGetDCNameInfo *info;
1687 bool ret;
1689 ret = wb_getdcname(p->mem_ctx,
1690 r->in.domainname,
1691 r->out.dcname,
1692 WBC_LOOKUP_DC_IS_FLAT_NAME |
1693 WBC_LOOKUP_DC_RETURN_FLAT_NAME |
1694 WBC_LOOKUP_DC_PDC_REQUIRED,
1695 &werr);
1696 if (ret == true) {
1697 return werr;
1700 flags = DS_PDC_REQUIRED | DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1702 status = dsgetdcname(p->mem_ctx,
1703 p->msg_ctx,
1704 r->in.domainname,
1705 NULL,
1706 NULL,
1707 flags,
1708 &info);
1709 if (!NT_STATUS_IS_OK(status)) {
1710 return ntstatus_to_werror(status);
1713 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1714 talloc_free(info);
1715 if (!*r->out.dcname) {
1716 return WERR_NOMEM;
1719 return WERR_OK;
1722 /****************************************************************
1723 _netr_GetAnyDCName
1724 ****************************************************************/
1726 WERROR _netr_GetAnyDCName(struct pipes_struct *p,
1727 struct netr_GetAnyDCName *r)
1729 NTSTATUS status;
1730 WERROR werr;
1731 uint32_t flags;
1732 struct netr_DsRGetDCNameInfo *info;
1733 bool ret;
1735 ret = wb_getdcname(p->mem_ctx,
1736 r->in.domainname,
1737 r->out.dcname,
1738 WBC_LOOKUP_DC_IS_FLAT_NAME |
1739 WBC_LOOKUP_DC_RETURN_FLAT_NAME,
1740 &werr);
1741 if (ret == true) {
1742 return werr;
1745 flags = DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1747 status = dsgetdcname(p->mem_ctx,
1748 p->msg_ctx,
1749 r->in.domainname,
1750 NULL,
1751 NULL,
1752 flags,
1753 &info);
1754 if (!NT_STATUS_IS_OK(status)) {
1755 return ntstatus_to_werror(status);
1758 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1759 talloc_free(info);
1760 if (!*r->out.dcname) {
1761 return WERR_NOMEM;
1764 return WERR_OK;
1767 /****************************************************************
1768 ****************************************************************/
1770 NTSTATUS _netr_DatabaseSync2(struct pipes_struct *p,
1771 struct netr_DatabaseSync2 *r)
1773 p->rng_fault_state = true;
1774 return NT_STATUS_NOT_IMPLEMENTED;
1777 /****************************************************************
1778 ****************************************************************/
1780 NTSTATUS _netr_DatabaseRedo(struct pipes_struct *p,
1781 struct netr_DatabaseRedo *r)
1783 p->rng_fault_state = true;
1784 return NT_STATUS_NOT_IMPLEMENTED;
1787 /****************************************************************
1788 ****************************************************************/
1790 WERROR _netr_DsRGetDCName(struct pipes_struct *p,
1791 struct netr_DsRGetDCName *r)
1793 p->rng_fault_state = true;
1794 return WERR_NOT_SUPPORTED;
1797 /****************************************************************
1798 ****************************************************************/
1800 NTSTATUS _netr_LogonGetCapabilities(struct pipes_struct *p,
1801 struct netr_LogonGetCapabilities *r)
1803 return NT_STATUS_NOT_IMPLEMENTED;
1806 /****************************************************************
1807 ****************************************************************/
1809 WERROR _netr_NETRLOGONSETSERVICEBITS(struct pipes_struct *p,
1810 struct netr_NETRLOGONSETSERVICEBITS *r)
1812 p->rng_fault_state = true;
1813 return WERR_NOT_SUPPORTED;
1816 /****************************************************************
1817 ****************************************************************/
1819 WERROR _netr_LogonGetTrustRid(struct pipes_struct *p,
1820 struct netr_LogonGetTrustRid *r)
1822 p->rng_fault_state = true;
1823 return WERR_NOT_SUPPORTED;
1826 /****************************************************************
1827 ****************************************************************/
1829 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(struct pipes_struct *p,
1830 struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
1832 p->rng_fault_state = true;
1833 return WERR_NOT_SUPPORTED;
1836 /****************************************************************
1837 ****************************************************************/
1839 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(struct pipes_struct *p,
1840 struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
1842 p->rng_fault_state = true;
1843 return WERR_NOT_SUPPORTED;
1846 /****************************************************************
1847 ****************************************************************/
1849 WERROR _netr_DsRGetDCNameEx(struct pipes_struct *p,
1850 struct netr_DsRGetDCNameEx *r)
1852 p->rng_fault_state = true;
1853 return WERR_NOT_SUPPORTED;
1856 /****************************************************************
1857 ****************************************************************/
1859 WERROR _netr_DsRGetSiteName(struct pipes_struct *p,
1860 struct netr_DsRGetSiteName *r)
1862 p->rng_fault_state = true;
1863 return WERR_NOT_SUPPORTED;
1866 /****************************************************************
1867 ****************************************************************/
1869 NTSTATUS _netr_LogonGetDomainInfo(struct pipes_struct *p,
1870 struct netr_LogonGetDomainInfo *r)
1872 p->rng_fault_state = true;
1873 return NT_STATUS_NOT_IMPLEMENTED;
1876 /****************************************************************
1877 ****************************************************************/
1879 WERROR _netr_ServerPasswordGet(struct pipes_struct *p,
1880 struct netr_ServerPasswordGet *r)
1882 p->rng_fault_state = true;
1883 return WERR_NOT_SUPPORTED;
1886 /****************************************************************
1887 ****************************************************************/
1889 WERROR _netr_NETRLOGONSENDTOSAM(struct pipes_struct *p,
1890 struct netr_NETRLOGONSENDTOSAM *r)
1892 p->rng_fault_state = true;
1893 return WERR_NOT_SUPPORTED;
1896 /****************************************************************
1897 ****************************************************************/
1899 WERROR _netr_DsRAddressToSitenamesW(struct pipes_struct *p,
1900 struct netr_DsRAddressToSitenamesW *r)
1902 p->rng_fault_state = true;
1903 return WERR_NOT_SUPPORTED;
1906 /****************************************************************
1907 ****************************************************************/
1909 WERROR _netr_DsRGetDCNameEx2(struct pipes_struct *p,
1910 struct netr_DsRGetDCNameEx2 *r)
1912 p->rng_fault_state = true;
1913 return WERR_NOT_SUPPORTED;
1916 /****************************************************************
1917 ****************************************************************/
1919 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(struct pipes_struct *p,
1920 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
1922 p->rng_fault_state = true;
1923 return WERR_NOT_SUPPORTED;
1926 /****************************************************************
1927 ****************************************************************/
1929 WERROR _netr_NetrEnumerateTrustedDomainsEx(struct pipes_struct *p,
1930 struct netr_NetrEnumerateTrustedDomainsEx *r)
1932 p->rng_fault_state = true;
1933 return WERR_NOT_SUPPORTED;
1936 /****************************************************************
1937 ****************************************************************/
1939 WERROR _netr_DsRAddressToSitenamesExW(struct pipes_struct *p,
1940 struct netr_DsRAddressToSitenamesExW *r)
1942 p->rng_fault_state = true;
1943 return WERR_NOT_SUPPORTED;
1946 /****************************************************************
1947 ****************************************************************/
1949 WERROR _netr_DsrGetDcSiteCoverageW(struct pipes_struct *p,
1950 struct netr_DsrGetDcSiteCoverageW *r)
1952 p->rng_fault_state = true;
1953 return WERR_NOT_SUPPORTED;
1956 /****************************************************************
1957 ****************************************************************/
1959 WERROR _netr_DsrEnumerateDomainTrusts(struct pipes_struct *p,
1960 struct netr_DsrEnumerateDomainTrusts *r)
1962 p->rng_fault_state = true;
1963 return WERR_NOT_SUPPORTED;
1966 /****************************************************************
1967 ****************************************************************/
1969 WERROR _netr_DsrDeregisterDNSHostRecords(struct pipes_struct *p,
1970 struct netr_DsrDeregisterDNSHostRecords *r)
1972 p->rng_fault_state = true;
1973 return WERR_NOT_SUPPORTED;
1976 /****************************************************************
1977 ****************************************************************/
1979 NTSTATUS _netr_ServerTrustPasswordsGet(struct pipes_struct *p,
1980 struct netr_ServerTrustPasswordsGet *r)
1982 p->rng_fault_state = true;
1983 return NT_STATUS_NOT_IMPLEMENTED;
1986 /****************************************************************
1987 ****************************************************************/
1989 WERROR _netr_DsRGetForestTrustInformation(struct pipes_struct *p,
1990 struct netr_DsRGetForestTrustInformation *r)
1992 p->rng_fault_state = true;
1993 return WERR_NOT_SUPPORTED;
1996 /****************************************************************
1997 ****************************************************************/
1999 NTSTATUS _netr_GetForestTrustInformation(struct pipes_struct *p,
2000 struct netr_GetForestTrustInformation *r)
2002 p->rng_fault_state = true;
2003 return NT_STATUS_NOT_IMPLEMENTED;
2006 /****************************************************************
2007 ****************************************************************/
2009 NTSTATUS _netr_ServerGetTrustInfo(struct pipes_struct *p,
2010 struct netr_ServerGetTrustInfo *r)
2012 p->rng_fault_state = true;
2013 return NT_STATUS_NOT_IMPLEMENTED;