s3-rpc: running minimal_includes.pl on rpc_client and rpc_server.
[Samba.git] / source3 / rpc_server / srv_netlog_nt.c
blob8f4381cde1a6635e8a0c785de7723ecdea04e658
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"
31 extern userdom_struct current_user_info;
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_RPC_SRV
36 struct netlogon_server_pipe_state {
37 struct netr_Credential client_challenge;
38 struct netr_Credential server_challenge;
41 /*************************************************************************
42 _netr_LogonControl
43 *************************************************************************/
45 WERROR _netr_LogonControl(pipes_struct *p,
46 struct netr_LogonControl *r)
48 struct netr_LogonControl2Ex l;
50 switch (r->in.level) {
51 case 1:
52 break;
53 case 2:
54 return WERR_NOT_SUPPORTED;
55 default:
56 return WERR_UNKNOWN_LEVEL;
59 l.in.logon_server = r->in.logon_server;
60 l.in.function_code = r->in.function_code;
61 l.in.level = r->in.level;
62 l.in.data = NULL;
63 l.out.query = r->out.query;
65 return _netr_LogonControl2Ex(p, &l);
68 /****************************************************************************
69 Send a message to smbd to do a sam synchronisation
70 **************************************************************************/
72 static void send_sync_message(void)
74 DEBUG(3, ("sending sam synchronisation message\n"));
75 message_send_all(smbd_messaging_context(), MSG_SMB_SAM_SYNC, NULL, 0,
76 NULL);
79 /*************************************************************************
80 _netr_LogonControl2
81 *************************************************************************/
83 WERROR _netr_LogonControl2(pipes_struct *p,
84 struct netr_LogonControl2 *r)
86 struct netr_LogonControl2Ex l;
88 l.in.logon_server = r->in.logon_server;
89 l.in.function_code = r->in.function_code;
90 l.in.level = r->in.level;
91 l.in.data = r->in.data;
92 l.out.query = r->out.query;
94 return _netr_LogonControl2Ex(p, &l);
97 /*************************************************************************
98 *************************************************************************/
100 static bool wb_change_trust_creds(const char *domain, WERROR *tc_status)
102 wbcErr result;
103 struct wbcAuthErrorInfo *error = NULL;
105 result = wbcChangeTrustCredentials(domain, &error);
106 switch (result) {
107 case WBC_ERR_WINBIND_NOT_AVAILABLE:
108 return false;
109 case WBC_ERR_DOMAIN_NOT_FOUND:
110 *tc_status = WERR_NO_SUCH_DOMAIN;
111 return true;
112 case WBC_ERR_SUCCESS:
113 *tc_status = WERR_OK;
114 return true;
115 default:
116 break;
119 if (error && error->nt_status != 0) {
120 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
121 } else {
122 *tc_status = WERR_TRUST_FAILURE;
124 wbcFreeMemory(error);
125 return true;
128 /*************************************************************************
129 *************************************************************************/
131 static bool wb_check_trust_creds(const char *domain, WERROR *tc_status)
133 wbcErr result;
134 struct wbcAuthErrorInfo *error = NULL;
136 result = wbcCheckTrustCredentials(domain, &error);
137 switch (result) {
138 case WBC_ERR_WINBIND_NOT_AVAILABLE:
139 return false;
140 case WBC_ERR_DOMAIN_NOT_FOUND:
141 *tc_status = WERR_NO_SUCH_DOMAIN;
142 return true;
143 case WBC_ERR_SUCCESS:
144 *tc_status = WERR_OK;
145 return true;
146 default:
147 break;
150 if (error && error->nt_status != 0) {
151 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
152 } else {
153 *tc_status = WERR_TRUST_FAILURE;
155 wbcFreeMemory(error);
156 return true;
159 /****************************************************************
160 _netr_LogonControl2Ex
161 ****************************************************************/
163 WERROR _netr_LogonControl2Ex(pipes_struct *p,
164 struct netr_LogonControl2Ex *r)
166 uint32_t flags = 0x0;
167 WERROR pdc_connection_status = WERR_OK;
168 uint32_t logon_attempts = 0x0;
169 WERROR tc_status;
170 fstring dc_name2;
171 const char *dc_name = NULL;
172 struct sockaddr_storage dc_ss;
173 const char *domain = NULL;
174 struct netr_NETLOGON_INFO_1 *info1;
175 struct netr_NETLOGON_INFO_2 *info2;
176 struct netr_NETLOGON_INFO_3 *info3;
177 struct netr_NETLOGON_INFO_4 *info4;
178 const char *fn;
179 uint32_t acct_ctrl;
181 switch (p->hdr_req.opnum) {
182 case NDR_NETR_LOGONCONTROL:
183 fn = "_netr_LogonControl";
184 break;
185 case NDR_NETR_LOGONCONTROL2:
186 fn = "_netr_LogonControl2";
187 break;
188 case NDR_NETR_LOGONCONTROL2EX:
189 fn = "_netr_LogonControl2Ex";
190 break;
191 default:
192 return WERR_INVALID_PARAM;
195 acct_ctrl = pdb_get_acct_ctrl(p->server_info->sam_account);
197 switch (r->in.function_code) {
198 case NETLOGON_CONTROL_TC_VERIFY:
199 case NETLOGON_CONTROL_CHANGE_PASSWORD:
200 case NETLOGON_CONTROL_REDISCOVER:
201 if ((geteuid() != sec_initial_uid()) &&
202 !nt_token_check_domain_rid(p->server_info->ptok, DOMAIN_RID_ADMINS) &&
203 !nt_token_check_sid(&global_sid_Builtin_Administrators, p->server_info->ptok) &&
204 !(acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST))) {
205 return WERR_ACCESS_DENIED;
207 break;
208 default:
209 break;
212 tc_status = WERR_NO_SUCH_DOMAIN;
214 switch (r->in.function_code) {
215 case NETLOGON_CONTROL_QUERY:
216 tc_status = WERR_OK;
217 break;
218 case NETLOGON_CONTROL_REPLICATE:
219 case NETLOGON_CONTROL_SYNCHRONIZE:
220 case NETLOGON_CONTROL_PDC_REPLICATE:
221 case NETLOGON_CONTROL_BACKUP_CHANGE_LOG:
222 case NETLOGON_CONTROL_BREAKPOINT:
223 if (acct_ctrl & ACB_NORMAL) {
224 return WERR_NOT_SUPPORTED;
225 } else if (acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST)) {
226 return WERR_ACCESS_DENIED;
227 } else {
228 return WERR_ACCESS_DENIED;
230 case NETLOGON_CONTROL_TRUNCATE_LOG:
231 if (acct_ctrl & ACB_NORMAL) {
232 break;
233 } else if (acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST)) {
234 return WERR_ACCESS_DENIED;
235 } else {
236 return WERR_ACCESS_DENIED;
239 case NETLOGON_CONTROL_TRANSPORT_NOTIFY:
240 case NETLOGON_CONTROL_FORCE_DNS_REG:
241 case NETLOGON_CONTROL_QUERY_DNS_REG:
242 return WERR_NOT_SUPPORTED;
243 case NETLOGON_CONTROL_FIND_USER:
244 if (!r->in.data || !r->in.data->user) {
245 return WERR_NOT_SUPPORTED;
247 break;
248 case NETLOGON_CONTROL_SET_DBFLAG:
249 if (!r->in.data) {
250 return WERR_NOT_SUPPORTED;
252 break;
253 case NETLOGON_CONTROL_TC_VERIFY:
254 if (!r->in.data || !r->in.data->domain) {
255 return WERR_NOT_SUPPORTED;
258 if (!wb_check_trust_creds(r->in.data->domain, &tc_status)) {
259 return WERR_NOT_SUPPORTED;
261 break;
262 case NETLOGON_CONTROL_TC_QUERY:
263 if (!r->in.data || !r->in.data->domain) {
264 return WERR_NOT_SUPPORTED;
267 domain = r->in.data->domain;
269 if (!is_trusted_domain(domain)) {
270 break;
273 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
274 tc_status = WERR_NO_LOGON_SERVERS;
275 break;
278 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
279 if (!dc_name) {
280 return WERR_NOMEM;
283 tc_status = WERR_OK;
285 break;
287 case NETLOGON_CONTROL_REDISCOVER:
288 if (!r->in.data || !r->in.data->domain) {
289 return WERR_NOT_SUPPORTED;
292 domain = r->in.data->domain;
294 if (!is_trusted_domain(domain)) {
295 break;
298 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
299 tc_status = WERR_NO_LOGON_SERVERS;
300 break;
303 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
304 if (!dc_name) {
305 return WERR_NOMEM;
308 tc_status = WERR_OK;
310 break;
312 case NETLOGON_CONTROL_CHANGE_PASSWORD:
313 if (!r->in.data || !r->in.data->domain) {
314 return WERR_NOT_SUPPORTED;
317 if (!wb_change_trust_creds(r->in.data->domain, &tc_status)) {
318 return WERR_NOT_SUPPORTED;
320 break;
322 default:
323 /* no idea what this should be */
324 DEBUG(0,("%s: unimplemented function level [%d]\n",
325 fn, r->in.function_code));
326 return WERR_UNKNOWN_LEVEL;
329 /* prepare the response */
331 switch (r->in.level) {
332 case 1:
333 info1 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_1);
334 W_ERROR_HAVE_NO_MEMORY(info1);
336 info1->flags = flags;
337 info1->pdc_connection_status = pdc_connection_status;
339 r->out.query->info1 = info1;
340 break;
341 case 2:
342 info2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_2);
343 W_ERROR_HAVE_NO_MEMORY(info2);
345 info2->flags = flags;
346 info2->pdc_connection_status = pdc_connection_status;
347 info2->trusted_dc_name = dc_name;
348 info2->tc_connection_status = tc_status;
350 r->out.query->info2 = info2;
351 break;
352 case 3:
353 info3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_3);
354 W_ERROR_HAVE_NO_MEMORY(info3);
356 info3->flags = flags;
357 info3->logon_attempts = logon_attempts;
359 r->out.query->info3 = info3;
360 break;
361 case 4:
362 info4 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_4);
363 W_ERROR_HAVE_NO_MEMORY(info4);
365 info4->trusted_dc_name = dc_name;
366 info4->trusted_domain_name = r->in.data->domain;
368 r->out.query->info4 = info4;
369 break;
370 default:
371 return WERR_UNKNOWN_LEVEL;
374 if (lp_server_role() == ROLE_DOMAIN_BDC) {
375 send_sync_message();
378 return WERR_OK;
381 /*************************************************************************
382 _netr_NetrEnumerateTrustedDomains
383 *************************************************************************/
385 WERROR _netr_NetrEnumerateTrustedDomains(pipes_struct *p,
386 struct netr_NetrEnumerateTrustedDomains *r)
388 NTSTATUS status;
389 DATA_BLOB blob;
390 struct trustdom_info **domains;
391 uint32_t num_domains;
392 const char **trusted_domains;
393 int i;
395 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
397 /* set up the Trusted Domain List response */
399 become_root();
400 status = pdb_enum_trusteddoms(p->mem_ctx, &num_domains, &domains);
401 unbecome_root();
403 if (!NT_STATUS_IS_OK(status)) {
404 return ntstatus_to_werror(status);
407 trusted_domains = talloc_zero_array(p->mem_ctx, const char *, num_domains + 1);
408 if (!trusted_domains) {
409 return WERR_NOMEM;
412 for (i = 0; i < num_domains; i++) {
413 trusted_domains[i] = talloc_strdup(trusted_domains, domains[i]->name);
414 if (!trusted_domains[i]) {
415 TALLOC_FREE(trusted_domains);
416 return WERR_NOMEM;
420 if (!push_reg_multi_sz(trusted_domains, &blob, trusted_domains)) {
421 TALLOC_FREE(trusted_domains);
422 return WERR_NOMEM;
425 r->out.trusted_domains_blob->data = blob.data;
426 r->out.trusted_domains_blob->length = blob.length;
428 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
430 return WERR_OK;
433 /******************************************************************
434 gets a machine password entry. checks access rights of the host.
435 ******************************************************************/
437 static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
438 enum netr_SchannelType sec_chan_type, struct dom_sid *sid)
440 struct samu *sampass = NULL;
441 const uint8 *pass;
442 bool ret;
443 uint32 acct_ctrl;
445 #if 0
446 char addr[INET6_ADDRSTRLEN];
449 * Currently this code is redundent as we already have a filter
450 * by hostname list. What this code really needs to do is to
451 * get a hosts allowed/hosts denied list from the SAM database
452 * on a per user basis, and make the access decision there.
453 * I will leave this code here for now as a reminder to implement
454 * this at a later date. JRA.
457 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
458 client_name(get_client_fd()),
459 client_addr(get_client_fd(),addr,sizeof(addr)))) {
460 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
461 return False;
463 #endif /* 0 */
465 if ( !(sampass = samu_new( NULL )) ) {
466 return NT_STATUS_NO_MEMORY;
469 /* JRA. This is ok as it is only used for generating the challenge. */
470 become_root();
471 ret = pdb_getsampwnam(sampass, mach_acct);
472 unbecome_root();
474 if (!ret) {
475 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
476 TALLOC_FREE(sampass);
477 return NT_STATUS_ACCESS_DENIED;
480 acct_ctrl = pdb_get_acct_ctrl(sampass);
481 if (acct_ctrl & ACB_DISABLED) {
482 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
483 TALLOC_FREE(sampass);
484 return NT_STATUS_ACCOUNT_DISABLED;
487 if (!(acct_ctrl & ACB_SVRTRUST) &&
488 !(acct_ctrl & ACB_WSTRUST) &&
489 !(acct_ctrl & ACB_DOMTRUST))
491 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
492 TALLOC_FREE(sampass);
493 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
496 switch (sec_chan_type) {
497 case SEC_CHAN_BDC:
498 if (!(acct_ctrl & ACB_SVRTRUST)) {
499 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
500 "but not a server trust account\n", mach_acct));
501 TALLOC_FREE(sampass);
502 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
504 break;
505 case SEC_CHAN_WKSTA:
506 if (!(acct_ctrl & ACB_WSTRUST)) {
507 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
508 "but not a workstation trust account\n", mach_acct));
509 TALLOC_FREE(sampass);
510 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
512 break;
513 case SEC_CHAN_DOMAIN:
514 if (!(acct_ctrl & ACB_DOMTRUST)) {
515 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
516 "but not a interdomain trust account\n", mach_acct));
517 TALLOC_FREE(sampass);
518 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
520 break;
521 default:
522 break;
525 if ((pass = pdb_get_nt_passwd(sampass)) == NULL) {
526 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
527 TALLOC_FREE(sampass);
528 return NT_STATUS_LOGON_FAILURE;
531 memcpy(md4pw->hash, pass, 16);
532 dump_data(5, md4pw->hash, 16);
534 sid_copy(sid, pdb_get_user_sid(sampass));
536 TALLOC_FREE(sampass);
538 return NT_STATUS_OK;
543 /*************************************************************************
544 _netr_ServerReqChallenge
545 *************************************************************************/
547 NTSTATUS _netr_ServerReqChallenge(pipes_struct *p,
548 struct netr_ServerReqChallenge *r)
550 struct netlogon_server_pipe_state *pipe_state =
551 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
553 if (pipe_state) {
554 DEBUG(10,("_netr_ServerReqChallenge: new challenge requested. Clearing old state.\n"));
555 talloc_free(pipe_state);
556 p->private_data = NULL;
559 pipe_state = talloc(p, struct netlogon_server_pipe_state);
560 NT_STATUS_HAVE_NO_MEMORY(pipe_state);
562 pipe_state->client_challenge = *r->in.credentials;
564 generate_random_buffer(pipe_state->server_challenge.data,
565 sizeof(pipe_state->server_challenge.data));
567 *r->out.return_credentials = pipe_state->server_challenge;
569 p->private_data = pipe_state;
571 return NT_STATUS_OK;
574 /*************************************************************************
575 _netr_ServerAuthenticate
576 Create the initial credentials.
577 *************************************************************************/
579 NTSTATUS _netr_ServerAuthenticate(pipes_struct *p,
580 struct netr_ServerAuthenticate *r)
582 struct netr_ServerAuthenticate3 a;
583 uint32_t negotiate_flags = 0;
584 uint32_t rid;
586 a.in.server_name = r->in.server_name;
587 a.in.account_name = r->in.account_name;
588 a.in.secure_channel_type = r->in.secure_channel_type;
589 a.in.computer_name = r->in.computer_name;
590 a.in.credentials = r->in.credentials;
591 a.in.negotiate_flags = &negotiate_flags;
593 a.out.return_credentials = r->out.return_credentials;
594 a.out.rid = &rid;
595 a.out.negotiate_flags = &negotiate_flags;
597 return _netr_ServerAuthenticate3(p, &a);
601 /*************************************************************************
602 _netr_ServerAuthenticate3
603 *************************************************************************/
605 NTSTATUS _netr_ServerAuthenticate3(pipes_struct *p,
606 struct netr_ServerAuthenticate3 *r)
608 NTSTATUS status;
609 uint32_t srv_flgs;
610 /* r->in.negotiate_flags is an aliased pointer to r->out.negotiate_flags,
611 * so use a copy to avoid destroying the client values. */
612 uint32_t in_neg_flags = *r->in.negotiate_flags;
613 const char *fn;
614 struct dom_sid sid;
615 struct samr_Password mach_pwd;
616 struct netlogon_creds_CredentialState *creds;
617 struct netlogon_server_pipe_state *pipe_state =
618 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
620 /* According to Microsoft (see bugid #6099)
621 * Windows 7 looks at the negotiate_flags
622 * returned in this structure *even if the
623 * call fails with access denied* ! So in order
624 * to allow Win7 to connect to a Samba NT style
625 * PDC we set the flags before we know if it's
626 * an error or not.
629 /* 0x000001ff */
630 srv_flgs = NETLOGON_NEG_ACCOUNT_LOCKOUT |
631 NETLOGON_NEG_PERSISTENT_SAMREPL |
632 NETLOGON_NEG_ARCFOUR |
633 NETLOGON_NEG_PROMOTION_COUNT |
634 NETLOGON_NEG_CHANGELOG_BDC |
635 NETLOGON_NEG_FULL_SYNC_REPL |
636 NETLOGON_NEG_MULTIPLE_SIDS |
637 NETLOGON_NEG_REDO |
638 NETLOGON_NEG_PASSWORD_CHANGE_REFUSAL |
639 NETLOGON_NEG_PASSWORD_SET2;
641 /* Ensure we support strong (128-bit) keys. */
642 if (in_neg_flags & NETLOGON_NEG_STRONG_KEYS) {
643 srv_flgs |= NETLOGON_NEG_STRONG_KEYS;
646 if (lp_server_schannel() != false) {
647 srv_flgs |= NETLOGON_NEG_SCHANNEL;
650 switch (p->hdr_req.opnum) {
651 case NDR_NETR_SERVERAUTHENTICATE:
652 fn = "_netr_ServerAuthenticate";
653 break;
654 case NDR_NETR_SERVERAUTHENTICATE2:
655 fn = "_netr_ServerAuthenticate2";
656 break;
657 case NDR_NETR_SERVERAUTHENTICATE3:
658 fn = "_netr_ServerAuthenticate3";
659 break;
660 default:
661 return NT_STATUS_INTERNAL_ERROR;
664 /* We use this as the key to store the creds: */
665 /* r->in.computer_name */
667 if (!pipe_state) {
668 DEBUG(0,("%s: no challenge sent to client %s\n", fn,
669 r->in.computer_name));
670 status = NT_STATUS_ACCESS_DENIED;
671 goto out;
674 if ( (lp_server_schannel() == true) &&
675 ((in_neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
677 /* schannel must be used, but client did not offer it. */
678 DEBUG(0,("%s: schannel required but client failed "
679 "to offer it. Client was %s\n",
680 fn, r->in.account_name));
681 status = NT_STATUS_ACCESS_DENIED;
682 goto out;
685 status = get_md4pw(&mach_pwd,
686 r->in.account_name,
687 r->in.secure_channel_type,
688 &sid);
689 if (!NT_STATUS_IS_OK(status)) {
690 DEBUG(0,("%s: failed to get machine password for "
691 "account %s: %s\n",
692 fn, r->in.account_name, nt_errstr(status) ));
693 /* always return NT_STATUS_ACCESS_DENIED */
694 status = NT_STATUS_ACCESS_DENIED;
695 goto out;
698 /* From the client / server challenges and md4 password, generate sess key */
699 /* Check client credentials are valid. */
700 creds = netlogon_creds_server_init(p->mem_ctx,
701 r->in.account_name,
702 r->in.computer_name,
703 r->in.secure_channel_type,
704 &pipe_state->client_challenge,
705 &pipe_state->server_challenge,
706 &mach_pwd,
707 r->in.credentials,
708 r->out.return_credentials,
709 *r->in.negotiate_flags);
710 if (!creds) {
711 DEBUG(0,("%s: netlogon_creds_server_check failed. Rejecting auth "
712 "request from client %s machine account %s\n",
713 fn, r->in.computer_name,
714 r->in.account_name));
715 status = NT_STATUS_ACCESS_DENIED;
716 goto out;
719 creds->sid = sid_dup_talloc(creds, &sid);
720 if (!creds->sid) {
721 status = NT_STATUS_NO_MEMORY;
722 goto out;
725 /* Store off the state so we can continue after client disconnect. */
726 become_root();
727 status = schannel_store_session_key(p->mem_ctx, creds);
728 unbecome_root();
730 if (!NT_STATUS_IS_OK(status)) {
731 goto out;
734 sid_peek_rid(&sid, r->out.rid);
736 status = NT_STATUS_OK;
738 out:
740 *r->out.negotiate_flags = srv_flgs;
741 return status;
744 /*************************************************************************
745 _netr_ServerAuthenticate2
746 *************************************************************************/
748 NTSTATUS _netr_ServerAuthenticate2(pipes_struct *p,
749 struct netr_ServerAuthenticate2 *r)
751 struct netr_ServerAuthenticate3 a;
752 uint32_t rid;
754 a.in.server_name = r->in.server_name;
755 a.in.account_name = r->in.account_name;
756 a.in.secure_channel_type = r->in.secure_channel_type;
757 a.in.computer_name = r->in.computer_name;
758 a.in.credentials = r->in.credentials;
759 a.in.negotiate_flags = r->in.negotiate_flags;
761 a.out.return_credentials = r->out.return_credentials;
762 a.out.rid = &rid;
763 a.out.negotiate_flags = r->out.negotiate_flags;
765 return _netr_ServerAuthenticate3(p, &a);
768 /*************************************************************************
769 *************************************************************************/
771 static NTSTATUS netr_creds_server_step_check(pipes_struct *p,
772 TALLOC_CTX *mem_ctx,
773 const char *computer_name,
774 struct netr_Authenticator *received_authenticator,
775 struct netr_Authenticator *return_authenticator,
776 struct netlogon_creds_CredentialState **creds_out)
778 NTSTATUS status;
779 struct tdb_context *tdb;
780 bool schannel_global_required = (lp_server_schannel() == true) ? true:false;
781 bool schannel_in_use = (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL) ? true:false; /* &&
782 (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY ||
783 p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY); */
785 tdb = open_schannel_session_store(mem_ctx);
786 if (!tdb) {
787 return NT_STATUS_ACCESS_DENIED;
790 status = schannel_creds_server_step_check_tdb(tdb, mem_ctx,
791 computer_name,
792 schannel_global_required,
793 schannel_in_use,
794 received_authenticator,
795 return_authenticator,
796 creds_out);
797 tdb_close(tdb);
799 return status;
802 /*************************************************************************
803 *************************************************************************/
805 static NTSTATUS netr_find_machine_account(TALLOC_CTX *mem_ctx,
806 const char *account_name,
807 struct samu **sampassp)
809 struct samu *sampass;
810 bool ret = false;
811 uint32_t acct_ctrl;
813 sampass = samu_new(mem_ctx);
814 if (!sampass) {
815 return NT_STATUS_NO_MEMORY;
818 become_root();
819 ret = pdb_getsampwnam(sampass, account_name);
820 unbecome_root();
822 if (!ret) {
823 TALLOC_FREE(sampass);
824 return NT_STATUS_ACCESS_DENIED;
827 /* Ensure the account exists and is a machine account. */
829 acct_ctrl = pdb_get_acct_ctrl(sampass);
831 if (!(acct_ctrl & ACB_WSTRUST ||
832 acct_ctrl & ACB_SVRTRUST ||
833 acct_ctrl & ACB_DOMTRUST)) {
834 TALLOC_FREE(sampass);
835 return NT_STATUS_NO_SUCH_USER;
838 if (acct_ctrl & ACB_DISABLED) {
839 TALLOC_FREE(sampass);
840 return NT_STATUS_ACCOUNT_DISABLED;
843 *sampassp = sampass;
845 return NT_STATUS_OK;
848 /*************************************************************************
849 *************************************************************************/
851 static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
852 struct samu *sampass,
853 DATA_BLOB *plaintext_blob,
854 struct samr_Password *nt_hash,
855 struct samr_Password *lm_hash)
857 NTSTATUS status;
858 const uchar *old_pw;
859 const char *plaintext = NULL;
860 size_t plaintext_len;
861 struct samr_Password nt_hash_local;
863 if (!sampass) {
864 return NT_STATUS_INVALID_PARAMETER;
867 if (plaintext_blob) {
868 if (!convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
869 plaintext_blob->data, plaintext_blob->length,
870 &plaintext, &plaintext_len, false))
872 plaintext = NULL;
873 mdfour(nt_hash_local.hash, plaintext_blob->data, plaintext_blob->length);
874 nt_hash = &nt_hash_local;
878 if (plaintext) {
879 if (!pdb_set_plaintext_passwd(sampass, plaintext)) {
880 return NT_STATUS_ACCESS_DENIED;
883 goto done;
886 if (nt_hash) {
887 old_pw = pdb_get_nt_passwd(sampass);
889 if (old_pw && memcmp(nt_hash->hash, old_pw, 16) == 0) {
890 /* Avoid backend modificiations and other fun if the
891 client changed the password to the *same thing* */
892 } else {
893 /* LM password should be NULL for machines */
894 if (!pdb_set_lanman_passwd(sampass, NULL, PDB_CHANGED)) {
895 return NT_STATUS_NO_MEMORY;
897 if (!pdb_set_nt_passwd(sampass, nt_hash->hash, PDB_CHANGED)) {
898 return NT_STATUS_NO_MEMORY;
901 if (!pdb_set_pass_last_set_time(sampass, time(NULL), PDB_CHANGED)) {
902 /* Not quite sure what this one qualifies as, but this will do */
903 return NT_STATUS_UNSUCCESSFUL;
908 done:
909 become_root();
910 status = pdb_update_sam_account(sampass);
911 unbecome_root();
913 return status;
916 /*************************************************************************
917 _netr_ServerPasswordSet
918 *************************************************************************/
920 NTSTATUS _netr_ServerPasswordSet(pipes_struct *p,
921 struct netr_ServerPasswordSet *r)
923 NTSTATUS status = NT_STATUS_OK;
924 struct samu *sampass=NULL;
925 int i;
926 struct netlogon_creds_CredentialState *creds;
928 DEBUG(5,("_netr_ServerPasswordSet: %d\n", __LINE__));
930 become_root();
931 status = netr_creds_server_step_check(p, p->mem_ctx,
932 r->in.computer_name,
933 r->in.credential,
934 r->out.return_authenticator,
935 &creds);
936 unbecome_root();
938 if (!NT_STATUS_IS_OK(status)) {
939 DEBUG(2,("_netr_ServerPasswordSet: netlogon_creds_server_step failed. Rejecting auth "
940 "request from client %s machine account %s\n",
941 r->in.computer_name, creds->computer_name));
942 TALLOC_FREE(creds);
943 return status;
946 DEBUG(3,("_netr_ServerPasswordSet: Server Password Set by remote machine:[%s] on account [%s]\n",
947 r->in.computer_name, creds->computer_name));
949 netlogon_creds_des_decrypt(creds, r->in.new_password);
951 DEBUG(100,("_netr_ServerPasswordSet: new given value was :\n"));
952 for(i = 0; i < sizeof(r->in.new_password->hash); i++)
953 DEBUG(100,("%02X ", r->in.new_password->hash[i]));
954 DEBUG(100,("\n"));
956 status = netr_find_machine_account(p->mem_ctx,
957 creds->account_name,
958 &sampass);
959 if (!NT_STATUS_IS_OK(status)) {
960 return status;
963 status = netr_set_machine_account_password(sampass,
964 sampass,
965 NULL,
966 r->in.new_password,
967 NULL);
968 TALLOC_FREE(sampass);
969 return status;
972 /****************************************************************
973 _netr_ServerPasswordSet2
974 ****************************************************************/
976 NTSTATUS _netr_ServerPasswordSet2(pipes_struct *p,
977 struct netr_ServerPasswordSet2 *r)
979 NTSTATUS status;
980 struct netlogon_creds_CredentialState *creds;
981 struct samu *sampass;
982 DATA_BLOB plaintext;
983 struct samr_CryptPassword password_buf;
984 struct samr_Password nt_hash;
986 become_root();
987 status = netr_creds_server_step_check(p, p->mem_ctx,
988 r->in.computer_name,
989 r->in.credential,
990 r->out.return_authenticator,
991 &creds);
992 unbecome_root();
994 if (!NT_STATUS_IS_OK(status)) {
995 DEBUG(2,("_netr_ServerPasswordSet2: netlogon_creds_server_step "
996 "failed. Rejecting auth request from client %s machine account %s\n",
997 r->in.computer_name, creds->computer_name));
998 TALLOC_FREE(creds);
999 return status;
1002 memcpy(password_buf.data, r->in.new_password->data, 512);
1003 SIVAL(password_buf.data, 512, r->in.new_password->length);
1004 netlogon_creds_arcfour_crypt(creds, password_buf.data, 516);
1006 if (!extract_pw_from_buffer(p->mem_ctx, password_buf.data, &plaintext)) {
1007 return NT_STATUS_WRONG_PASSWORD;
1010 mdfour(nt_hash.hash, plaintext.data, plaintext.length);
1012 status = netr_find_machine_account(p->mem_ctx,
1013 creds->account_name,
1014 &sampass);
1015 if (!NT_STATUS_IS_OK(status)) {
1016 return status;
1019 status = netr_set_machine_account_password(sampass,
1020 sampass,
1021 NULL,
1022 &nt_hash,
1023 NULL);
1024 TALLOC_FREE(sampass);
1025 return status;
1028 /*************************************************************************
1029 _netr_LogonSamLogoff
1030 *************************************************************************/
1032 NTSTATUS _netr_LogonSamLogoff(pipes_struct *p,
1033 struct netr_LogonSamLogoff *r)
1035 NTSTATUS status;
1036 struct netlogon_creds_CredentialState *creds;
1038 become_root();
1039 status = netr_creds_server_step_check(p, p->mem_ctx,
1040 r->in.computer_name,
1041 r->in.credential,
1042 r->out.return_authenticator,
1043 &creds);
1044 unbecome_root();
1046 return status;
1049 /*************************************************************************
1050 _netr_LogonSamLogon_base
1051 *************************************************************************/
1053 static NTSTATUS _netr_LogonSamLogon_base(pipes_struct *p,
1054 struct netr_LogonSamLogonEx *r,
1055 struct netlogon_creds_CredentialState *creds)
1057 NTSTATUS status = NT_STATUS_OK;
1058 union netr_LogonLevel *logon = r->in.logon;
1059 const char *nt_username, *nt_domain, *nt_workstation;
1060 auth_usersupplied_info *user_info = NULL;
1061 auth_serversupplied_info *server_info = NULL;
1062 struct auth_context *auth_context = NULL;
1063 uint8_t pipe_session_key[16];
1064 bool process_creds = true;
1065 const char *fn;
1067 switch (p->hdr_req.opnum) {
1068 case NDR_NETR_LOGONSAMLOGON:
1069 process_creds = true;
1070 fn = "_netr_LogonSamLogon";
1071 break;
1072 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
1073 process_creds = true;
1074 fn = "_netr_LogonSamLogonWithFlags";
1075 break;
1076 case NDR_NETR_LOGONSAMLOGONEX:
1077 process_creds = false;
1078 fn = "_netr_LogonSamLogonEx";
1079 break;
1080 default:
1081 return NT_STATUS_INTERNAL_ERROR;
1084 *r->out.authoritative = true; /* authoritative response */
1086 switch (r->in.validation_level) {
1087 case 2:
1088 r->out.validation->sam2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo2);
1089 if (!r->out.validation->sam2) {
1090 return NT_STATUS_NO_MEMORY;
1092 break;
1093 case 3:
1094 r->out.validation->sam3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo3);
1095 if (!r->out.validation->sam3) {
1096 return NT_STATUS_NO_MEMORY;
1098 break;
1099 case 6:
1100 r->out.validation->sam6 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo6);
1101 if (!r->out.validation->sam6) {
1102 return NT_STATUS_NO_MEMORY;
1104 break;
1105 default:
1106 DEBUG(0,("%s: bad validation_level value %d.\n",
1107 fn, (int)r->in.validation_level));
1108 return NT_STATUS_INVALID_INFO_CLASS;
1111 switch (r->in.logon_level) {
1112 case NetlogonInteractiveInformation:
1113 case NetlogonServiceInformation:
1114 case NetlogonInteractiveTransitiveInformation:
1115 case NetlogonServiceTransitiveInformation:
1116 nt_username = logon->password->identity_info.account_name.string;
1117 nt_domain = logon->password->identity_info.domain_name.string;
1118 nt_workstation = logon->password->identity_info.workstation.string;
1120 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
1121 break;
1122 case NetlogonNetworkInformation:
1123 case NetlogonNetworkTransitiveInformation:
1124 nt_username = logon->network->identity_info.account_name.string;
1125 nt_domain = logon->network->identity_info.domain_name.string;
1126 nt_workstation = logon->network->identity_info.workstation.string;
1128 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
1129 break;
1130 default:
1131 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1132 return NT_STATUS_INVALID_INFO_CLASS;
1133 } /* end switch */
1135 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
1136 fstrcpy(current_user_info.smb_name, nt_username);
1137 sub_set_smb_name(nt_username);
1139 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
1140 r->in.validation_level, nt_username));
1142 status = NT_STATUS_OK;
1144 switch (r->in.logon_level) {
1145 case NetlogonNetworkInformation:
1146 case NetlogonNetworkTransitiveInformation:
1148 const char *wksname = nt_workstation;
1150 status = make_auth_context_fixed(&auth_context,
1151 logon->network->challenge);
1152 if (!NT_STATUS_IS_OK(status)) {
1153 return status;
1156 /* For a network logon, the workstation name comes in with two
1157 * backslashes in the front. Strip them if they are there. */
1159 if (*wksname == '\\') wksname++;
1160 if (*wksname == '\\') wksname++;
1162 /* Standard challenge/response authenticaion */
1163 if (!make_user_info_netlogon_network(&user_info,
1164 nt_username, nt_domain,
1165 wksname,
1166 logon->network->identity_info.parameter_control,
1167 logon->network->lm.data,
1168 logon->network->lm.length,
1169 logon->network->nt.data,
1170 logon->network->nt.length)) {
1171 status = NT_STATUS_NO_MEMORY;
1173 break;
1175 case NetlogonInteractiveInformation:
1176 case NetlogonServiceInformation:
1177 case NetlogonInteractiveTransitiveInformation:
1178 case NetlogonServiceTransitiveInformation:
1180 /* 'Interactive' authentication, supplies the password in its
1181 MD4 form, encrypted with the session key. We will convert
1182 this to challenge/response for the auth subsystem to chew
1183 on */
1185 uint8_t chal[8];
1187 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
1188 return status;
1191 auth_context->get_ntlm_challenge(auth_context, chal);
1193 if (!make_user_info_netlogon_interactive(&user_info,
1194 nt_username, nt_domain,
1195 nt_workstation,
1196 logon->password->identity_info.parameter_control,
1197 chal,
1198 logon->password->lmpassword.hash,
1199 logon->password->ntpassword.hash,
1200 creds->session_key)) {
1201 status = NT_STATUS_NO_MEMORY;
1203 break;
1205 default:
1206 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1207 return NT_STATUS_INVALID_INFO_CLASS;
1208 } /* end switch */
1210 if ( NT_STATUS_IS_OK(status) ) {
1211 status = auth_context->check_ntlm_password(auth_context,
1212 user_info, &server_info);
1215 (auth_context->free)(&auth_context);
1216 free_user_info(&user_info);
1218 DEBUG(5,("%s: check_password returned status %s\n",
1219 fn, nt_errstr(status)));
1221 /* Check account and password */
1223 if (!NT_STATUS_IS_OK(status)) {
1224 /* If we don't know what this domain is, we need to
1225 indicate that we are not authoritative. This
1226 allows the client to decide if it needs to try
1227 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
1228 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1229 && !strequal(nt_domain, get_global_sam_name())
1230 && !is_trusted_domain(nt_domain) )
1231 *r->out.authoritative = false; /* We are not authoritative */
1233 TALLOC_FREE(server_info);
1234 return status;
1237 if (server_info->guest) {
1238 /* We don't like guest domain logons... */
1239 DEBUG(5,("%s: Attempted domain logon as GUEST "
1240 "denied.\n", fn));
1241 TALLOC_FREE(server_info);
1242 return NT_STATUS_LOGON_FAILURE;
1245 /* This is the point at which, if the login was successful, that
1246 the SAM Local Security Authority should record that the user is
1247 logged in to the domain. */
1249 if (process_creds) {
1250 /* Get the pipe session key from the creds. */
1251 memcpy(pipe_session_key, creds->session_key, 16);
1252 } else {
1253 /* Get the pipe session key from the schannel. */
1254 if ((p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL)
1255 || (p->auth.a_u.schannel_auth == NULL)) {
1256 return NT_STATUS_INVALID_HANDLE;
1258 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->creds->session_key, 16);
1261 switch (r->in.validation_level) {
1262 case 2:
1263 status = serverinfo_to_SamInfo2(server_info, pipe_session_key, 16,
1264 r->out.validation->sam2);
1265 break;
1266 case 3:
1267 status = serverinfo_to_SamInfo3(server_info, pipe_session_key, 16,
1268 r->out.validation->sam3);
1269 break;
1270 case 6:
1271 status = serverinfo_to_SamInfo6(server_info, pipe_session_key, 16,
1272 r->out.validation->sam6);
1273 break;
1276 TALLOC_FREE(server_info);
1278 return status;
1281 /****************************************************************
1282 _netr_LogonSamLogonWithFlags
1283 ****************************************************************/
1285 NTSTATUS _netr_LogonSamLogonWithFlags(pipes_struct *p,
1286 struct netr_LogonSamLogonWithFlags *r)
1288 NTSTATUS status;
1289 struct netlogon_creds_CredentialState *creds;
1290 struct netr_LogonSamLogonEx r2;
1291 struct netr_Authenticator return_authenticator;
1293 become_root();
1294 status = netr_creds_server_step_check(p, p->mem_ctx,
1295 r->in.computer_name,
1296 r->in.credential,
1297 &return_authenticator,
1298 &creds);
1299 unbecome_root();
1300 if (!NT_STATUS_IS_OK(status)) {
1301 return status;
1304 r2.in.server_name = r->in.server_name;
1305 r2.in.computer_name = r->in.computer_name;
1306 r2.in.logon_level = r->in.logon_level;
1307 r2.in.logon = r->in.logon;
1308 r2.in.validation_level = r->in.validation_level;
1309 r2.in.flags = r->in.flags;
1310 r2.out.validation = r->out.validation;
1311 r2.out.authoritative = r->out.authoritative;
1312 r2.out.flags = r->out.flags;
1314 status = _netr_LogonSamLogon_base(p, &r2, creds);
1316 *r->out.return_authenticator = return_authenticator;
1318 return status;
1321 /*************************************************************************
1322 _netr_LogonSamLogon
1323 *************************************************************************/
1325 NTSTATUS _netr_LogonSamLogon(pipes_struct *p,
1326 struct netr_LogonSamLogon *r)
1328 NTSTATUS status;
1329 struct netr_LogonSamLogonWithFlags r2;
1330 uint32_t flags = 0;
1332 r2.in.server_name = r->in.server_name;
1333 r2.in.computer_name = r->in.computer_name;
1334 r2.in.credential = r->in.credential;
1335 r2.in.logon_level = r->in.logon_level;
1336 r2.in.logon = r->in.logon;
1337 r2.in.validation_level = r->in.validation_level;
1338 r2.in.return_authenticator = r->in.return_authenticator;
1339 r2.in.flags = &flags;
1340 r2.out.validation = r->out.validation;
1341 r2.out.authoritative = r->out.authoritative;
1342 r2.out.flags = &flags;
1343 r2.out.return_authenticator = r->out.return_authenticator;
1345 status = _netr_LogonSamLogonWithFlags(p, &r2);
1347 return status;
1350 /*************************************************************************
1351 _netr_LogonSamLogonEx
1352 - no credential chaining. Map into net sam logon.
1353 *************************************************************************/
1355 NTSTATUS _netr_LogonSamLogonEx(pipes_struct *p,
1356 struct netr_LogonSamLogonEx *r)
1358 NTSTATUS status;
1359 struct netlogon_creds_CredentialState *creds = NULL;
1361 become_root();
1362 status = schannel_fetch_session_key(p->mem_ctx, r->in.computer_name, &creds);
1363 unbecome_root();
1364 if (!NT_STATUS_IS_OK(status)) {
1365 return status;
1368 /* Only allow this if the pipe is protected. */
1369 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1370 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1371 get_remote_machine_name() ));
1372 return NT_STATUS_INVALID_PARAMETER;
1375 status = _netr_LogonSamLogon_base(p, r, creds);
1376 TALLOC_FREE(creds);
1378 return status;
1381 /*************************************************************************
1382 _ds_enum_dom_trusts
1383 *************************************************************************/
1384 #if 0 /* JERRY -- not correct */
1385 NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1386 DS_R_ENUM_DOM_TRUSTS *r_u)
1388 NTSTATUS status = NT_STATUS_OK;
1390 /* TODO: According to MSDN, the can only be executed against a
1391 DC or domain member running Windows 2000 or later. Need
1392 to test against a standalone 2k server and see what it
1393 does. A windows 2000 DC includes its own domain in the
1394 list. --jerry */
1396 return status;
1398 #endif /* JERRY */
1401 /****************************************************************
1402 ****************************************************************/
1404 WERROR _netr_LogonUasLogon(pipes_struct *p,
1405 struct netr_LogonUasLogon *r)
1407 p->rng_fault_state = true;
1408 return WERR_NOT_SUPPORTED;
1411 /****************************************************************
1412 ****************************************************************/
1414 WERROR _netr_LogonUasLogoff(pipes_struct *p,
1415 struct netr_LogonUasLogoff *r)
1417 p->rng_fault_state = true;
1418 return WERR_NOT_SUPPORTED;
1421 /****************************************************************
1422 ****************************************************************/
1424 NTSTATUS _netr_DatabaseDeltas(pipes_struct *p,
1425 struct netr_DatabaseDeltas *r)
1427 p->rng_fault_state = true;
1428 return NT_STATUS_NOT_IMPLEMENTED;
1431 /****************************************************************
1432 ****************************************************************/
1434 NTSTATUS _netr_DatabaseSync(pipes_struct *p,
1435 struct netr_DatabaseSync *r)
1437 p->rng_fault_state = true;
1438 return NT_STATUS_NOT_IMPLEMENTED;
1441 /****************************************************************
1442 ****************************************************************/
1444 NTSTATUS _netr_AccountDeltas(pipes_struct *p,
1445 struct netr_AccountDeltas *r)
1447 p->rng_fault_state = true;
1448 return NT_STATUS_NOT_IMPLEMENTED;
1451 /****************************************************************
1452 ****************************************************************/
1454 NTSTATUS _netr_AccountSync(pipes_struct *p,
1455 struct netr_AccountSync *r)
1457 p->rng_fault_state = true;
1458 return NT_STATUS_NOT_IMPLEMENTED;
1461 /****************************************************************
1462 ****************************************************************/
1464 static bool wb_getdcname(TALLOC_CTX *mem_ctx,
1465 const char *domain,
1466 const char **dcname,
1467 uint32_t flags,
1468 WERROR *werr)
1470 wbcErr result;
1471 struct wbcDomainControllerInfo *dc_info = NULL;
1473 result = wbcLookupDomainController(domain,
1474 flags,
1475 &dc_info);
1476 switch (result) {
1477 case WBC_ERR_SUCCESS:
1478 break;
1479 case WBC_ERR_WINBIND_NOT_AVAILABLE:
1480 return false;
1481 case WBC_ERR_DOMAIN_NOT_FOUND:
1482 *werr = WERR_NO_SUCH_DOMAIN;
1483 return true;
1484 default:
1485 *werr = WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1486 return true;
1489 *dcname = talloc_strdup(mem_ctx, dc_info->dc_name);
1490 wbcFreeMemory(dc_info);
1491 if (!*dcname) {
1492 *werr = WERR_NOMEM;
1493 return false;
1496 *werr = WERR_OK;
1498 return true;
1501 /****************************************************************
1502 _netr_GetDcName
1503 ****************************************************************/
1505 WERROR _netr_GetDcName(pipes_struct *p,
1506 struct netr_GetDcName *r)
1508 NTSTATUS status;
1509 WERROR werr;
1510 uint32_t flags;
1511 struct netr_DsRGetDCNameInfo *info;
1512 bool ret;
1514 ret = wb_getdcname(p->mem_ctx,
1515 r->in.domainname,
1516 r->out.dcname,
1517 WBC_LOOKUP_DC_IS_FLAT_NAME |
1518 WBC_LOOKUP_DC_RETURN_FLAT_NAME |
1519 WBC_LOOKUP_DC_PDC_REQUIRED,
1520 &werr);
1521 if (ret == true) {
1522 return werr;
1525 flags = DS_PDC_REQUIRED | DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1527 status = dsgetdcname(p->mem_ctx,
1528 smbd_messaging_context(),
1529 r->in.domainname,
1530 NULL,
1531 NULL,
1532 flags,
1533 &info);
1534 if (!NT_STATUS_IS_OK(status)) {
1535 return ntstatus_to_werror(status);
1538 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1539 talloc_free(info);
1540 if (!*r->out.dcname) {
1541 return WERR_NOMEM;
1544 return WERR_OK;
1547 /****************************************************************
1548 _netr_GetAnyDCName
1549 ****************************************************************/
1551 WERROR _netr_GetAnyDCName(pipes_struct *p,
1552 struct netr_GetAnyDCName *r)
1554 NTSTATUS status;
1555 WERROR werr;
1556 uint32_t flags;
1557 struct netr_DsRGetDCNameInfo *info;
1558 bool ret;
1560 ret = wb_getdcname(p->mem_ctx,
1561 r->in.domainname,
1562 r->out.dcname,
1563 WBC_LOOKUP_DC_IS_FLAT_NAME |
1564 WBC_LOOKUP_DC_RETURN_FLAT_NAME,
1565 &werr);
1566 if (ret == true) {
1567 return werr;
1570 flags = DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1572 status = dsgetdcname(p->mem_ctx,
1573 smbd_messaging_context(),
1574 r->in.domainname,
1575 NULL,
1576 NULL,
1577 flags,
1578 &info);
1579 if (!NT_STATUS_IS_OK(status)) {
1580 return ntstatus_to_werror(status);
1583 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1584 talloc_free(info);
1585 if (!*r->out.dcname) {
1586 return WERR_NOMEM;
1589 return WERR_OK;
1592 /****************************************************************
1593 ****************************************************************/
1595 NTSTATUS _netr_DatabaseSync2(pipes_struct *p,
1596 struct netr_DatabaseSync2 *r)
1598 p->rng_fault_state = true;
1599 return NT_STATUS_NOT_IMPLEMENTED;
1602 /****************************************************************
1603 ****************************************************************/
1605 NTSTATUS _netr_DatabaseRedo(pipes_struct *p,
1606 struct netr_DatabaseRedo *r)
1608 p->rng_fault_state = true;
1609 return NT_STATUS_NOT_IMPLEMENTED;
1612 /****************************************************************
1613 ****************************************************************/
1615 WERROR _netr_DsRGetDCName(pipes_struct *p,
1616 struct netr_DsRGetDCName *r)
1618 p->rng_fault_state = true;
1619 return WERR_NOT_SUPPORTED;
1622 /****************************************************************
1623 ****************************************************************/
1625 NTSTATUS _netr_LogonGetCapabilities(pipes_struct *p,
1626 struct netr_LogonGetCapabilities *r)
1628 return NT_STATUS_NOT_IMPLEMENTED;
1631 /****************************************************************
1632 ****************************************************************/
1634 WERROR _netr_NETRLOGONSETSERVICEBITS(pipes_struct *p,
1635 struct netr_NETRLOGONSETSERVICEBITS *r)
1637 p->rng_fault_state = true;
1638 return WERR_NOT_SUPPORTED;
1641 /****************************************************************
1642 ****************************************************************/
1644 WERROR _netr_LogonGetTrustRid(pipes_struct *p,
1645 struct netr_LogonGetTrustRid *r)
1647 p->rng_fault_state = true;
1648 return WERR_NOT_SUPPORTED;
1651 /****************************************************************
1652 ****************************************************************/
1654 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(pipes_struct *p,
1655 struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
1657 p->rng_fault_state = true;
1658 return WERR_NOT_SUPPORTED;
1661 /****************************************************************
1662 ****************************************************************/
1664 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(pipes_struct *p,
1665 struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
1667 p->rng_fault_state = true;
1668 return WERR_NOT_SUPPORTED;
1671 /****************************************************************
1672 ****************************************************************/
1674 WERROR _netr_DsRGetDCNameEx(pipes_struct *p,
1675 struct netr_DsRGetDCNameEx *r)
1677 p->rng_fault_state = true;
1678 return WERR_NOT_SUPPORTED;
1681 /****************************************************************
1682 ****************************************************************/
1684 WERROR _netr_DsRGetSiteName(pipes_struct *p,
1685 struct netr_DsRGetSiteName *r)
1687 p->rng_fault_state = true;
1688 return WERR_NOT_SUPPORTED;
1691 /****************************************************************
1692 ****************************************************************/
1694 NTSTATUS _netr_LogonGetDomainInfo(pipes_struct *p,
1695 struct netr_LogonGetDomainInfo *r)
1697 p->rng_fault_state = true;
1698 return NT_STATUS_NOT_IMPLEMENTED;
1701 /****************************************************************
1702 ****************************************************************/
1704 WERROR _netr_ServerPasswordGet(pipes_struct *p,
1705 struct netr_ServerPasswordGet *r)
1707 p->rng_fault_state = true;
1708 return WERR_NOT_SUPPORTED;
1711 /****************************************************************
1712 ****************************************************************/
1714 WERROR _netr_NETRLOGONSENDTOSAM(pipes_struct *p,
1715 struct netr_NETRLOGONSENDTOSAM *r)
1717 p->rng_fault_state = true;
1718 return WERR_NOT_SUPPORTED;
1721 /****************************************************************
1722 ****************************************************************/
1724 WERROR _netr_DsRAddressToSitenamesW(pipes_struct *p,
1725 struct netr_DsRAddressToSitenamesW *r)
1727 p->rng_fault_state = true;
1728 return WERR_NOT_SUPPORTED;
1731 /****************************************************************
1732 ****************************************************************/
1734 WERROR _netr_DsRGetDCNameEx2(pipes_struct *p,
1735 struct netr_DsRGetDCNameEx2 *r)
1737 p->rng_fault_state = true;
1738 return WERR_NOT_SUPPORTED;
1741 /****************************************************************
1742 ****************************************************************/
1744 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(pipes_struct *p,
1745 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
1747 p->rng_fault_state = true;
1748 return WERR_NOT_SUPPORTED;
1751 /****************************************************************
1752 ****************************************************************/
1754 WERROR _netr_NetrEnumerateTrustedDomainsEx(pipes_struct *p,
1755 struct netr_NetrEnumerateTrustedDomainsEx *r)
1757 p->rng_fault_state = true;
1758 return WERR_NOT_SUPPORTED;
1761 /****************************************************************
1762 ****************************************************************/
1764 WERROR _netr_DsRAddressToSitenamesExW(pipes_struct *p,
1765 struct netr_DsRAddressToSitenamesExW *r)
1767 p->rng_fault_state = true;
1768 return WERR_NOT_SUPPORTED;
1771 /****************************************************************
1772 ****************************************************************/
1774 WERROR _netr_DsrGetDcSiteCoverageW(pipes_struct *p,
1775 struct netr_DsrGetDcSiteCoverageW *r)
1777 p->rng_fault_state = true;
1778 return WERR_NOT_SUPPORTED;
1781 /****************************************************************
1782 ****************************************************************/
1784 WERROR _netr_DsrEnumerateDomainTrusts(pipes_struct *p,
1785 struct netr_DsrEnumerateDomainTrusts *r)
1787 p->rng_fault_state = true;
1788 return WERR_NOT_SUPPORTED;
1791 /****************************************************************
1792 ****************************************************************/
1794 WERROR _netr_DsrDeregisterDNSHostRecords(pipes_struct *p,
1795 struct netr_DsrDeregisterDNSHostRecords *r)
1797 p->rng_fault_state = true;
1798 return WERR_NOT_SUPPORTED;
1801 /****************************************************************
1802 ****************************************************************/
1804 NTSTATUS _netr_ServerTrustPasswordsGet(pipes_struct *p,
1805 struct netr_ServerTrustPasswordsGet *r)
1807 p->rng_fault_state = true;
1808 return NT_STATUS_NOT_IMPLEMENTED;
1811 /****************************************************************
1812 ****************************************************************/
1814 WERROR _netr_DsRGetForestTrustInformation(pipes_struct *p,
1815 struct netr_DsRGetForestTrustInformation *r)
1817 p->rng_fault_state = true;
1818 return WERR_NOT_SUPPORTED;
1821 /****************************************************************
1822 ****************************************************************/
1824 WERROR _netr_GetForestTrustInformation(pipes_struct *p,
1825 struct netr_GetForestTrustInformation *r)
1827 p->rng_fault_state = true;
1828 return WERR_NOT_SUPPORTED;
1831 /****************************************************************
1832 ****************************************************************/
1834 NTSTATUS _netr_ServerGetTrustInfo(pipes_struct *p,
1835 struct netr_ServerGetTrustInfo *r)
1837 p->rng_fault_state = true;
1838 return NT_STATUS_NOT_IMPLEMENTED;