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.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 /* This is the implementation of the netlogon pipe. */
28 #include "../libcli/auth/libcli_auth.h"
29 #include "../libcli/auth/schannel_state.h"
30 #include "../libcli/auth/schannel.h"
32 extern userdom_struct current_user_info;
35 #define DBGC_CLASS DBGC_RPC_SRV
37 struct netlogon_server_pipe_state {
38 struct netr_Credential client_challenge;
39 struct netr_Credential server_challenge;
42 /*************************************************************************
44 *************************************************************************/
46 WERROR _netr_LogonControl(pipes_struct *p,
47 struct netr_LogonControl *r)
49 struct netr_LogonControl2Ex l;
51 switch (r->in.level) {
55 return WERR_NOT_SUPPORTED;
57 return WERR_UNKNOWN_LEVEL;
60 l.in.logon_server = r->in.logon_server;
61 l.in.function_code = r->in.function_code;
62 l.in.level = r->in.level;
64 l.out.query = r->out.query;
66 return _netr_LogonControl2Ex(p, &l);
69 /****************************************************************************
70 Send a message to smbd to do a sam synchronisation
71 **************************************************************************/
73 static void send_sync_message(void)
75 DEBUG(3, ("sending sam synchronisation message\n"));
76 message_send_all(smbd_messaging_context(), MSG_SMB_SAM_SYNC, NULL, 0,
80 /*************************************************************************
82 *************************************************************************/
84 WERROR _netr_LogonControl2(pipes_struct *p,
85 struct netr_LogonControl2 *r)
87 struct netr_LogonControl2Ex l;
89 l.in.logon_server = r->in.logon_server;
90 l.in.function_code = r->in.function_code;
91 l.in.level = r->in.level;
92 l.in.data = r->in.data;
93 l.out.query = r->out.query;
95 return _netr_LogonControl2Ex(p, &l);
98 /*************************************************************************
99 *************************************************************************/
101 static bool wb_change_trust_creds(const char *domain, WERROR *tc_status)
104 struct wbcAuthErrorInfo *error = NULL;
106 result = wbcChangeTrustCredentials(domain, &error);
108 case WBC_ERR_WINBIND_NOT_AVAILABLE:
110 case WBC_ERR_DOMAIN_NOT_FOUND:
111 *tc_status = WERR_NO_SUCH_DOMAIN;
113 case WBC_ERR_SUCCESS:
114 *tc_status = WERR_OK;
120 if (error && error->nt_status != 0) {
121 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
123 *tc_status = WERR_TRUST_FAILURE;
125 wbcFreeMemory(error);
129 /****************************************************************
130 _netr_LogonControl2Ex
131 ****************************************************************/
133 WERROR _netr_LogonControl2Ex(pipes_struct *p,
134 struct netr_LogonControl2Ex *r)
136 uint32_t flags = 0x0;
137 WERROR pdc_connection_status = WERR_OK;
138 uint32_t logon_attempts = 0x0;
141 const char *dc_name = NULL;
142 struct sockaddr_storage dc_ss;
143 const char *domain = NULL;
144 struct netr_NETLOGON_INFO_1 *info1;
145 struct netr_NETLOGON_INFO_2 *info2;
146 struct netr_NETLOGON_INFO_3 *info3;
149 switch (p->hdr_req.opnum) {
150 case NDR_NETR_LOGONCONTROL:
151 fn = "_netr_LogonControl";
153 case NDR_NETR_LOGONCONTROL2:
154 fn = "_netr_LogonControl2";
156 case NDR_NETR_LOGONCONTROL2EX:
157 fn = "_netr_LogonControl2Ex";
160 return WERR_INVALID_PARAM;
163 tc_status = WERR_NO_SUCH_DOMAIN;
165 switch (r->in.function_code) {
166 case NETLOGON_CONTROL_TC_QUERY:
167 domain = r->in.data->domain;
169 if (!is_trusted_domain(domain)) {
173 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
174 tc_status = WERR_NO_LOGON_SERVERS;
178 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
187 case NETLOGON_CONTROL_REDISCOVER:
188 domain = r->in.data->domain;
190 if (!is_trusted_domain(domain)) {
194 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
195 tc_status = WERR_NO_LOGON_SERVERS;
199 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
208 case NETLOGON_CONTROL_CHANGE_PASSWORD:
209 if (!r->in.data || !r->in.data->domain) {
210 return WERR_NOT_SUPPORTED;
213 if (!wb_change_trust_creds(r->in.data->domain, &tc_status)) {
214 return WERR_NOT_SUPPORTED;
219 /* no idea what this should be */
220 DEBUG(0,("%s: unimplemented function level [%d]\n",
221 fn, r->in.function_code));
222 return WERR_UNKNOWN_LEVEL;
225 /* prepare the response */
227 switch (r->in.level) {
229 info1 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_1);
230 W_ERROR_HAVE_NO_MEMORY(info1);
232 info1->flags = flags;
233 info1->pdc_connection_status = pdc_connection_status;
235 r->out.query->info1 = info1;
238 info2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_2);
239 W_ERROR_HAVE_NO_MEMORY(info2);
241 info2->flags = flags;
242 info2->pdc_connection_status = pdc_connection_status;
243 info2->trusted_dc_name = dc_name;
244 info2->tc_connection_status = tc_status;
246 r->out.query->info2 = info2;
249 info3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_3);
250 W_ERROR_HAVE_NO_MEMORY(info3);
252 info3->flags = flags;
253 info3->logon_attempts = logon_attempts;
255 r->out.query->info3 = info3;
258 return WERR_UNKNOWN_LEVEL;
261 if (lp_server_role() == ROLE_DOMAIN_BDC) {
268 /*************************************************************************
269 _netr_NetrEnumerateTrustedDomains
270 *************************************************************************/
272 WERROR _netr_NetrEnumerateTrustedDomains(pipes_struct *p,
273 struct netr_NetrEnumerateTrustedDomains *r)
277 struct trustdom_info **domains;
278 uint32_t num_domains;
279 const char **trusted_domains;
282 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
284 /* set up the Trusted Domain List response */
287 status = pdb_enum_trusteddoms(p->mem_ctx, &num_domains, &domains);
290 if (!NT_STATUS_IS_OK(status)) {
291 return ntstatus_to_werror(status);
294 trusted_domains = talloc_zero_array(p->mem_ctx, const char *, num_domains + 1);
295 if (!trusted_domains) {
299 for (i = 0; i < num_domains; i++) {
300 trusted_domains[i] = talloc_strdup(trusted_domains, domains[i]->name);
301 if (!trusted_domains[i]) {
302 TALLOC_FREE(trusted_domains);
307 if (!push_reg_multi_sz(trusted_domains, &blob, trusted_domains)) {
308 TALLOC_FREE(trusted_domains);
312 r->out.trusted_domains_blob->data = blob.data;
313 r->out.trusted_domains_blob->length = blob.length;
315 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
320 /******************************************************************
321 gets a machine password entry. checks access rights of the host.
322 ******************************************************************/
324 static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
325 enum netr_SchannelType sec_chan_type, struct dom_sid *sid)
327 struct samu *sampass = NULL;
333 char addr[INET6_ADDRSTRLEN];
336 * Currently this code is redundent as we already have a filter
337 * by hostname list. What this code really needs to do is to
338 * get a hosts allowed/hosts denied list from the SAM database
339 * on a per user basis, and make the access decision there.
340 * I will leave this code here for now as a reminder to implement
341 * this at a later date. JRA.
344 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
345 client_name(get_client_fd()),
346 client_addr(get_client_fd(),addr,sizeof(addr)))) {
347 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
352 if ( !(sampass = samu_new( NULL )) ) {
353 return NT_STATUS_NO_MEMORY;
356 /* JRA. This is ok as it is only used for generating the challenge. */
358 ret = pdb_getsampwnam(sampass, mach_acct);
362 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
363 TALLOC_FREE(sampass);
364 return NT_STATUS_ACCESS_DENIED;
367 acct_ctrl = pdb_get_acct_ctrl(sampass);
368 if (acct_ctrl & ACB_DISABLED) {
369 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
370 TALLOC_FREE(sampass);
371 return NT_STATUS_ACCOUNT_DISABLED;
374 if (!(acct_ctrl & ACB_SVRTRUST) &&
375 !(acct_ctrl & ACB_WSTRUST) &&
376 !(acct_ctrl & ACB_DOMTRUST))
378 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
379 TALLOC_FREE(sampass);
380 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
383 switch (sec_chan_type) {
385 if (!(acct_ctrl & ACB_SVRTRUST)) {
386 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
387 "but not a server trust account\n", mach_acct));
388 TALLOC_FREE(sampass);
389 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
393 if (!(acct_ctrl & ACB_WSTRUST)) {
394 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
395 "but not a workstation trust account\n", mach_acct));
396 TALLOC_FREE(sampass);
397 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
400 case SEC_CHAN_DOMAIN:
401 if (!(acct_ctrl & ACB_DOMTRUST)) {
402 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
403 "but not a interdomain trust account\n", mach_acct));
404 TALLOC_FREE(sampass);
405 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
412 if ((pass = pdb_get_nt_passwd(sampass)) == NULL) {
413 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
414 TALLOC_FREE(sampass);
415 return NT_STATUS_LOGON_FAILURE;
418 memcpy(md4pw->hash, pass, 16);
419 dump_data(5, md4pw->hash, 16);
421 sid_copy(sid, pdb_get_user_sid(sampass));
423 TALLOC_FREE(sampass);
430 /*************************************************************************
431 _netr_ServerReqChallenge
432 *************************************************************************/
434 NTSTATUS _netr_ServerReqChallenge(pipes_struct *p,
435 struct netr_ServerReqChallenge *r)
437 struct netlogon_server_pipe_state *pipe_state =
438 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
441 DEBUG(10,("_netr_ServerReqChallenge: new challenge requested. Clearing old state.\n"));
442 talloc_free(pipe_state);
443 p->private_data = NULL;
446 pipe_state = talloc(p, struct netlogon_server_pipe_state);
447 NT_STATUS_HAVE_NO_MEMORY(pipe_state);
449 pipe_state->client_challenge = *r->in.credentials;
451 generate_random_buffer(pipe_state->server_challenge.data,
452 sizeof(pipe_state->server_challenge.data));
454 *r->out.return_credentials = pipe_state->server_challenge;
456 p->private_data = pipe_state;
461 /*************************************************************************
462 _netr_ServerAuthenticate
463 Create the initial credentials.
464 *************************************************************************/
466 NTSTATUS _netr_ServerAuthenticate(pipes_struct *p,
467 struct netr_ServerAuthenticate *r)
469 struct netr_ServerAuthenticate3 a;
470 uint32_t negotiate_flags = 0;
473 a.in.server_name = r->in.server_name;
474 a.in.account_name = r->in.account_name;
475 a.in.secure_channel_type = r->in.secure_channel_type;
476 a.in.computer_name = r->in.computer_name;
477 a.in.credentials = r->in.credentials;
478 a.in.negotiate_flags = &negotiate_flags;
480 a.out.return_credentials = r->out.return_credentials;
482 a.out.negotiate_flags = &negotiate_flags;
484 return _netr_ServerAuthenticate3(p, &a);
488 /*************************************************************************
489 _netr_ServerAuthenticate3
490 *************************************************************************/
492 NTSTATUS _netr_ServerAuthenticate3(pipes_struct *p,
493 struct netr_ServerAuthenticate3 *r)
497 /* r->in.negotiate_flags is an aliased pointer to r->out.negotiate_flags,
498 * so use a copy to avoid destroying the client values. */
499 uint32_t in_neg_flags = *r->in.negotiate_flags;
502 struct samr_Password mach_pwd;
503 struct netlogon_creds_CredentialState *creds;
504 struct netlogon_server_pipe_state *pipe_state =
505 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
507 /* According to Microsoft (see bugid #6099)
508 * Windows 7 looks at the negotiate_flags
509 * returned in this structure *even if the
510 * call fails with access denied* ! So in order
511 * to allow Win7 to connect to a Samba NT style
512 * PDC we set the flags before we know if it's
517 srv_flgs = NETLOGON_NEG_ACCOUNT_LOCKOUT |
518 NETLOGON_NEG_PERSISTENT_SAMREPL |
519 NETLOGON_NEG_ARCFOUR |
520 NETLOGON_NEG_PROMOTION_COUNT |
521 NETLOGON_NEG_CHANGELOG_BDC |
522 NETLOGON_NEG_FULL_SYNC_REPL |
523 NETLOGON_NEG_MULTIPLE_SIDS |
525 NETLOGON_NEG_PASSWORD_CHANGE_REFUSAL |
526 NETLOGON_NEG_PASSWORD_SET2;
528 /* Ensure we support strong (128-bit) keys. */
529 if (in_neg_flags & NETLOGON_NEG_STRONG_KEYS) {
530 srv_flgs |= NETLOGON_NEG_STRONG_KEYS;
533 if (lp_server_schannel() != false) {
534 srv_flgs |= NETLOGON_NEG_SCHANNEL;
537 switch (p->hdr_req.opnum) {
538 case NDR_NETR_SERVERAUTHENTICATE:
539 fn = "_netr_ServerAuthenticate";
541 case NDR_NETR_SERVERAUTHENTICATE2:
542 fn = "_netr_ServerAuthenticate2";
544 case NDR_NETR_SERVERAUTHENTICATE3:
545 fn = "_netr_ServerAuthenticate3";
548 return NT_STATUS_INTERNAL_ERROR;
551 /* We use this as the key to store the creds: */
552 /* r->in.computer_name */
555 DEBUG(0,("%s: no challenge sent to client %s\n", fn,
556 r->in.computer_name));
557 status = NT_STATUS_ACCESS_DENIED;
561 if ( (lp_server_schannel() == true) &&
562 ((in_neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
564 /* schannel must be used, but client did not offer it. */
565 DEBUG(0,("%s: schannel required but client failed "
566 "to offer it. Client was %s\n",
567 fn, r->in.account_name));
568 status = NT_STATUS_ACCESS_DENIED;
572 status = get_md4pw(&mach_pwd,
574 r->in.secure_channel_type,
576 if (!NT_STATUS_IS_OK(status)) {
577 DEBUG(0,("%s: failed to get machine password for "
579 fn, r->in.account_name, nt_errstr(status) ));
580 /* always return NT_STATUS_ACCESS_DENIED */
581 status = NT_STATUS_ACCESS_DENIED;
585 /* From the client / server challenges and md4 password, generate sess key */
586 /* Check client credentials are valid. */
587 creds = netlogon_creds_server_init(p->mem_ctx,
590 r->in.secure_channel_type,
591 &pipe_state->client_challenge,
592 &pipe_state->server_challenge,
595 r->out.return_credentials,
596 *r->in.negotiate_flags);
598 DEBUG(0,("%s: netlogon_creds_server_check failed. Rejecting auth "
599 "request from client %s machine account %s\n",
600 fn, r->in.computer_name,
601 r->in.account_name));
602 status = NT_STATUS_ACCESS_DENIED;
606 creds->sid = sid_dup_talloc(creds, &sid);
608 status = NT_STATUS_NO_MEMORY;
612 /* Store off the state so we can continue after client disconnect. */
614 status = schannel_store_session_key(p->mem_ctx, creds);
617 if (!NT_STATUS_IS_OK(status)) {
621 sid_peek_rid(&sid, r->out.rid);
623 status = NT_STATUS_OK;
627 *r->out.negotiate_flags = srv_flgs;
631 /*************************************************************************
632 _netr_ServerAuthenticate2
633 *************************************************************************/
635 NTSTATUS _netr_ServerAuthenticate2(pipes_struct *p,
636 struct netr_ServerAuthenticate2 *r)
638 struct netr_ServerAuthenticate3 a;
641 a.in.server_name = r->in.server_name;
642 a.in.account_name = r->in.account_name;
643 a.in.secure_channel_type = r->in.secure_channel_type;
644 a.in.computer_name = r->in.computer_name;
645 a.in.credentials = r->in.credentials;
646 a.in.negotiate_flags = r->in.negotiate_flags;
648 a.out.return_credentials = r->out.return_credentials;
650 a.out.negotiate_flags = r->out.negotiate_flags;
652 return _netr_ServerAuthenticate3(p, &a);
655 /*************************************************************************
656 *************************************************************************/
658 static NTSTATUS netr_creds_server_step_check(pipes_struct *p,
660 const char *computer_name,
661 struct netr_Authenticator *received_authenticator,
662 struct netr_Authenticator *return_authenticator,
663 struct netlogon_creds_CredentialState **creds_out)
666 struct tdb_context *tdb;
667 bool schannel_global_required = (lp_server_schannel() == true) ? true:false;
668 bool schannel_in_use = (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL) ? true:false; /* &&
669 (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY ||
670 p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY); */
672 tdb = open_schannel_session_store(mem_ctx);
674 return NT_STATUS_ACCESS_DENIED;
677 status = schannel_creds_server_step_check_tdb(tdb, mem_ctx,
679 schannel_global_required,
681 received_authenticator,
682 return_authenticator,
689 /*************************************************************************
690 *************************************************************************/
692 static NTSTATUS netr_find_machine_account(TALLOC_CTX *mem_ctx,
693 const char *account_name,
694 struct samu **sampassp)
696 struct samu *sampass;
700 sampass = samu_new(mem_ctx);
702 return NT_STATUS_NO_MEMORY;
706 ret = pdb_getsampwnam(sampass, account_name);
710 TALLOC_FREE(sampass);
711 return NT_STATUS_ACCESS_DENIED;
714 /* Ensure the account exists and is a machine account. */
716 acct_ctrl = pdb_get_acct_ctrl(sampass);
718 if (!(acct_ctrl & ACB_WSTRUST ||
719 acct_ctrl & ACB_SVRTRUST ||
720 acct_ctrl & ACB_DOMTRUST)) {
721 TALLOC_FREE(sampass);
722 return NT_STATUS_NO_SUCH_USER;
725 if (acct_ctrl & ACB_DISABLED) {
726 TALLOC_FREE(sampass);
727 return NT_STATUS_ACCOUNT_DISABLED;
735 /*************************************************************************
736 *************************************************************************/
738 static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
739 struct samu *sampass,
740 DATA_BLOB *plaintext_blob,
741 struct samr_Password *nt_hash,
742 struct samr_Password *lm_hash)
746 const char *plaintext = NULL;
747 size_t plaintext_len;
748 struct samr_Password nt_hash_local;
751 return NT_STATUS_INVALID_PARAMETER;
754 if (plaintext_blob) {
755 if (!convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
756 plaintext_blob->data, plaintext_blob->length,
757 &plaintext, &plaintext_len, false))
760 mdfour(nt_hash_local.hash, plaintext_blob->data, plaintext_blob->length);
761 nt_hash = &nt_hash_local;
766 if (!pdb_set_plaintext_passwd(sampass, plaintext)) {
767 return NT_STATUS_ACCESS_DENIED;
774 old_pw = pdb_get_nt_passwd(sampass);
776 if (old_pw && memcmp(nt_hash->hash, old_pw, 16) == 0) {
777 /* Avoid backend modificiations and other fun if the
778 client changed the password to the *same thing* */
780 /* LM password should be NULL for machines */
781 if (!pdb_set_lanman_passwd(sampass, NULL, PDB_CHANGED)) {
782 return NT_STATUS_NO_MEMORY;
784 if (!pdb_set_nt_passwd(sampass, nt_hash->hash, PDB_CHANGED)) {
785 return NT_STATUS_NO_MEMORY;
788 if (!pdb_set_pass_last_set_time(sampass, time(NULL), PDB_CHANGED)) {
789 /* Not quite sure what this one qualifies as, but this will do */
790 return NT_STATUS_UNSUCCESSFUL;
797 status = pdb_update_sam_account(sampass);
803 /*************************************************************************
804 _netr_ServerPasswordSet
805 *************************************************************************/
807 NTSTATUS _netr_ServerPasswordSet(pipes_struct *p,
808 struct netr_ServerPasswordSet *r)
810 NTSTATUS status = NT_STATUS_OK;
811 struct samu *sampass=NULL;
813 struct netlogon_creds_CredentialState *creds;
815 DEBUG(5,("_netr_ServerPasswordSet: %d\n", __LINE__));
818 status = netr_creds_server_step_check(p, p->mem_ctx,
821 r->out.return_authenticator,
825 if (!NT_STATUS_IS_OK(status)) {
826 DEBUG(2,("_netr_ServerPasswordSet: netlogon_creds_server_step failed. Rejecting auth "
827 "request from client %s machine account %s\n",
828 r->in.computer_name, creds->computer_name));
833 DEBUG(3,("_netr_ServerPasswordSet: Server Password Set by remote machine:[%s] on account [%s]\n",
834 r->in.computer_name, creds->computer_name));
836 netlogon_creds_des_decrypt(creds, r->in.new_password);
838 DEBUG(100,("_netr_ServerPasswordSet: new given value was :\n"));
839 for(i = 0; i < sizeof(r->in.new_password->hash); i++)
840 DEBUG(100,("%02X ", r->in.new_password->hash[i]));
843 status = netr_find_machine_account(p->mem_ctx,
846 if (!NT_STATUS_IS_OK(status)) {
850 status = netr_set_machine_account_password(sampass,
855 TALLOC_FREE(sampass);
859 /****************************************************************
860 _netr_ServerPasswordSet2
861 ****************************************************************/
863 NTSTATUS _netr_ServerPasswordSet2(pipes_struct *p,
864 struct netr_ServerPasswordSet2 *r)
867 struct netlogon_creds_CredentialState *creds;
868 struct samu *sampass;
870 struct samr_CryptPassword password_buf;
871 struct samr_Password nt_hash;
874 status = netr_creds_server_step_check(p, p->mem_ctx,
877 r->out.return_authenticator,
881 if (!NT_STATUS_IS_OK(status)) {
882 DEBUG(2,("_netr_ServerPasswordSet2: netlogon_creds_server_step "
883 "failed. Rejecting auth request from client %s machine account %s\n",
884 r->in.computer_name, creds->computer_name));
889 memcpy(password_buf.data, r->in.new_password->data, 512);
890 SIVAL(password_buf.data, 512, r->in.new_password->length);
891 netlogon_creds_arcfour_crypt(creds, password_buf.data, 516);
893 if (!extract_pw_from_buffer(p->mem_ctx, password_buf.data, &plaintext)) {
894 return NT_STATUS_WRONG_PASSWORD;
897 mdfour(nt_hash.hash, plaintext.data, plaintext.length);
899 status = netr_find_machine_account(p->mem_ctx,
902 if (!NT_STATUS_IS_OK(status)) {
906 status = netr_set_machine_account_password(sampass,
911 TALLOC_FREE(sampass);
915 /*************************************************************************
917 *************************************************************************/
919 NTSTATUS _netr_LogonSamLogoff(pipes_struct *p,
920 struct netr_LogonSamLogoff *r)
923 struct netlogon_creds_CredentialState *creds;
926 status = netr_creds_server_step_check(p, p->mem_ctx,
929 r->out.return_authenticator,
936 /*************************************************************************
937 _netr_LogonSamLogon_base
938 *************************************************************************/
940 static NTSTATUS _netr_LogonSamLogon_base(pipes_struct *p,
941 struct netr_LogonSamLogonEx *r,
942 struct netlogon_creds_CredentialState *creds)
944 NTSTATUS status = NT_STATUS_OK;
945 union netr_LogonLevel *logon = r->in.logon;
946 const char *nt_username, *nt_domain, *nt_workstation;
947 auth_usersupplied_info *user_info = NULL;
948 auth_serversupplied_info *server_info = NULL;
949 struct auth_context *auth_context = NULL;
950 uint8_t pipe_session_key[16];
951 bool process_creds = true;
954 switch (p->hdr_req.opnum) {
955 case NDR_NETR_LOGONSAMLOGON:
956 process_creds = true;
957 fn = "_netr_LogonSamLogon";
959 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
960 process_creds = true;
961 fn = "_netr_LogonSamLogonWithFlags";
963 case NDR_NETR_LOGONSAMLOGONEX:
964 process_creds = false;
965 fn = "_netr_LogonSamLogonEx";
968 return NT_STATUS_INTERNAL_ERROR;
971 *r->out.authoritative = true; /* authoritative response */
973 switch (r->in.validation_level) {
975 r->out.validation->sam2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo2);
976 if (!r->out.validation->sam2) {
977 return NT_STATUS_NO_MEMORY;
981 r->out.validation->sam3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo3);
982 if (!r->out.validation->sam3) {
983 return NT_STATUS_NO_MEMORY;
987 r->out.validation->sam6 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo6);
988 if (!r->out.validation->sam6) {
989 return NT_STATUS_NO_MEMORY;
993 DEBUG(0,("%s: bad validation_level value %d.\n",
994 fn, (int)r->in.validation_level));
995 return NT_STATUS_INVALID_INFO_CLASS;
998 switch (r->in.logon_level) {
999 case NetlogonInteractiveInformation:
1000 case NetlogonServiceInformation:
1001 case NetlogonInteractiveTransitiveInformation:
1002 case NetlogonServiceTransitiveInformation:
1003 nt_username = logon->password->identity_info.account_name.string;
1004 nt_domain = logon->password->identity_info.domain_name.string;
1005 nt_workstation = logon->password->identity_info.workstation.string;
1007 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
1009 case NetlogonNetworkInformation:
1010 case NetlogonNetworkTransitiveInformation:
1011 nt_username = logon->network->identity_info.account_name.string;
1012 nt_domain = logon->network->identity_info.domain_name.string;
1013 nt_workstation = logon->network->identity_info.workstation.string;
1015 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
1018 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1019 return NT_STATUS_INVALID_INFO_CLASS;
1022 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
1023 fstrcpy(current_user_info.smb_name, nt_username);
1024 sub_set_smb_name(nt_username);
1026 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
1027 r->in.validation_level, nt_username));
1029 status = NT_STATUS_OK;
1031 switch (r->in.logon_level) {
1032 case NetlogonNetworkInformation:
1033 case NetlogonNetworkTransitiveInformation:
1035 const char *wksname = nt_workstation;
1037 status = make_auth_context_fixed(&auth_context,
1038 logon->network->challenge);
1039 if (!NT_STATUS_IS_OK(status)) {
1043 /* For a network logon, the workstation name comes in with two
1044 * backslashes in the front. Strip them if they are there. */
1046 if (*wksname == '\\') wksname++;
1047 if (*wksname == '\\') wksname++;
1049 /* Standard challenge/response authenticaion */
1050 if (!make_user_info_netlogon_network(&user_info,
1051 nt_username, nt_domain,
1053 logon->network->identity_info.parameter_control,
1054 logon->network->lm.data,
1055 logon->network->lm.length,
1056 logon->network->nt.data,
1057 logon->network->nt.length)) {
1058 status = NT_STATUS_NO_MEMORY;
1062 case NetlogonInteractiveInformation:
1063 case NetlogonServiceInformation:
1064 case NetlogonInteractiveTransitiveInformation:
1065 case NetlogonServiceTransitiveInformation:
1067 /* 'Interactive' authentication, supplies the password in its
1068 MD4 form, encrypted with the session key. We will convert
1069 this to challenge/response for the auth subsystem to chew
1074 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
1078 auth_context->get_ntlm_challenge(auth_context, chal);
1080 if (!make_user_info_netlogon_interactive(&user_info,
1081 nt_username, nt_domain,
1083 logon->password->identity_info.parameter_control,
1085 logon->password->lmpassword.hash,
1086 logon->password->ntpassword.hash,
1087 creds->session_key)) {
1088 status = NT_STATUS_NO_MEMORY;
1093 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1094 return NT_STATUS_INVALID_INFO_CLASS;
1097 if ( NT_STATUS_IS_OK(status) ) {
1098 status = auth_context->check_ntlm_password(auth_context,
1099 user_info, &server_info);
1102 (auth_context->free)(&auth_context);
1103 free_user_info(&user_info);
1105 DEBUG(5,("%s: check_password returned status %s\n",
1106 fn, nt_errstr(status)));
1108 /* Check account and password */
1110 if (!NT_STATUS_IS_OK(status)) {
1111 /* If we don't know what this domain is, we need to
1112 indicate that we are not authoritative. This
1113 allows the client to decide if it needs to try
1114 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
1115 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1116 && !strequal(nt_domain, get_global_sam_name())
1117 && !is_trusted_domain(nt_domain) )
1118 *r->out.authoritative = false; /* We are not authoritative */
1120 TALLOC_FREE(server_info);
1124 if (server_info->guest) {
1125 /* We don't like guest domain logons... */
1126 DEBUG(5,("%s: Attempted domain logon as GUEST "
1128 TALLOC_FREE(server_info);
1129 return NT_STATUS_LOGON_FAILURE;
1132 /* This is the point at which, if the login was successful, that
1133 the SAM Local Security Authority should record that the user is
1134 logged in to the domain. */
1136 if (process_creds) {
1137 /* Get the pipe session key from the creds. */
1138 memcpy(pipe_session_key, creds->session_key, 16);
1140 /* Get the pipe session key from the schannel. */
1141 if ((p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL)
1142 || (p->auth.a_u.schannel_auth == NULL)) {
1143 return NT_STATUS_INVALID_HANDLE;
1145 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->creds->session_key, 16);
1148 switch (r->in.validation_level) {
1150 status = serverinfo_to_SamInfo2(server_info, pipe_session_key, 16,
1151 r->out.validation->sam2);
1154 status = serverinfo_to_SamInfo3(server_info, pipe_session_key, 16,
1155 r->out.validation->sam3);
1158 status = serverinfo_to_SamInfo6(server_info, pipe_session_key, 16,
1159 r->out.validation->sam6);
1163 TALLOC_FREE(server_info);
1168 /****************************************************************
1169 _netr_LogonSamLogonWithFlags
1170 ****************************************************************/
1172 NTSTATUS _netr_LogonSamLogonWithFlags(pipes_struct *p,
1173 struct netr_LogonSamLogonWithFlags *r)
1176 struct netlogon_creds_CredentialState *creds;
1177 struct netr_LogonSamLogonEx r2;
1178 struct netr_Authenticator return_authenticator;
1181 status = netr_creds_server_step_check(p, p->mem_ctx,
1182 r->in.computer_name,
1184 &return_authenticator,
1187 if (!NT_STATUS_IS_OK(status)) {
1191 r2.in.server_name = r->in.server_name;
1192 r2.in.computer_name = r->in.computer_name;
1193 r2.in.logon_level = r->in.logon_level;
1194 r2.in.logon = r->in.logon;
1195 r2.in.validation_level = r->in.validation_level;
1196 r2.in.flags = r->in.flags;
1197 r2.out.validation = r->out.validation;
1198 r2.out.authoritative = r->out.authoritative;
1199 r2.out.flags = r->out.flags;
1201 status = _netr_LogonSamLogon_base(p, &r2, creds);
1203 *r->out.return_authenticator = return_authenticator;
1208 /*************************************************************************
1210 *************************************************************************/
1212 NTSTATUS _netr_LogonSamLogon(pipes_struct *p,
1213 struct netr_LogonSamLogon *r)
1216 struct netr_LogonSamLogonWithFlags r2;
1219 r2.in.server_name = r->in.server_name;
1220 r2.in.computer_name = r->in.computer_name;
1221 r2.in.credential = r->in.credential;
1222 r2.in.logon_level = r->in.logon_level;
1223 r2.in.logon = r->in.logon;
1224 r2.in.validation_level = r->in.validation_level;
1225 r2.in.return_authenticator = r->in.return_authenticator;
1226 r2.in.flags = &flags;
1227 r2.out.validation = r->out.validation;
1228 r2.out.authoritative = r->out.authoritative;
1229 r2.out.flags = &flags;
1230 r2.out.return_authenticator = r->out.return_authenticator;
1232 status = _netr_LogonSamLogonWithFlags(p, &r2);
1237 /*************************************************************************
1238 _netr_LogonSamLogonEx
1239 - no credential chaining. Map into net sam logon.
1240 *************************************************************************/
1242 NTSTATUS _netr_LogonSamLogonEx(pipes_struct *p,
1243 struct netr_LogonSamLogonEx *r)
1246 struct netlogon_creds_CredentialState *creds = NULL;
1249 status = schannel_fetch_session_key(p->mem_ctx, r->in.computer_name, &creds);
1251 if (!NT_STATUS_IS_OK(status)) {
1255 /* Only allow this if the pipe is protected. */
1256 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1257 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1258 get_remote_machine_name() ));
1259 return NT_STATUS_INVALID_PARAMETER;
1262 status = _netr_LogonSamLogon_base(p, r, creds);
1268 /*************************************************************************
1270 *************************************************************************/
1271 #if 0 /* JERRY -- not correct */
1272 NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1273 DS_R_ENUM_DOM_TRUSTS *r_u)
1275 NTSTATUS status = NT_STATUS_OK;
1277 /* TODO: According to MSDN, the can only be executed against a
1278 DC or domain member running Windows 2000 or later. Need
1279 to test against a standalone 2k server and see what it
1280 does. A windows 2000 DC includes its own domain in the
1288 /****************************************************************
1289 ****************************************************************/
1291 WERROR _netr_LogonUasLogon(pipes_struct *p,
1292 struct netr_LogonUasLogon *r)
1294 p->rng_fault_state = true;
1295 return WERR_NOT_SUPPORTED;
1298 /****************************************************************
1299 ****************************************************************/
1301 WERROR _netr_LogonUasLogoff(pipes_struct *p,
1302 struct netr_LogonUasLogoff *r)
1304 p->rng_fault_state = true;
1305 return WERR_NOT_SUPPORTED;
1308 /****************************************************************
1309 ****************************************************************/
1311 NTSTATUS _netr_DatabaseDeltas(pipes_struct *p,
1312 struct netr_DatabaseDeltas *r)
1314 p->rng_fault_state = true;
1315 return NT_STATUS_NOT_IMPLEMENTED;
1318 /****************************************************************
1319 ****************************************************************/
1321 NTSTATUS _netr_DatabaseSync(pipes_struct *p,
1322 struct netr_DatabaseSync *r)
1324 p->rng_fault_state = true;
1325 return NT_STATUS_NOT_IMPLEMENTED;
1328 /****************************************************************
1329 ****************************************************************/
1331 NTSTATUS _netr_AccountDeltas(pipes_struct *p,
1332 struct netr_AccountDeltas *r)
1334 p->rng_fault_state = true;
1335 return NT_STATUS_NOT_IMPLEMENTED;
1338 /****************************************************************
1339 ****************************************************************/
1341 NTSTATUS _netr_AccountSync(pipes_struct *p,
1342 struct netr_AccountSync *r)
1344 p->rng_fault_state = true;
1345 return NT_STATUS_NOT_IMPLEMENTED;
1348 /****************************************************************
1349 ****************************************************************/
1351 WERROR _netr_GetDcName(pipes_struct *p,
1352 struct netr_GetDcName *r)
1354 p->rng_fault_state = true;
1355 return WERR_NOT_SUPPORTED;
1358 /****************************************************************
1359 ****************************************************************/
1361 WERROR _netr_GetAnyDCName(pipes_struct *p,
1362 struct netr_GetAnyDCName *r)
1364 p->rng_fault_state = true;
1365 return WERR_NOT_SUPPORTED;
1368 /****************************************************************
1369 ****************************************************************/
1371 NTSTATUS _netr_DatabaseSync2(pipes_struct *p,
1372 struct netr_DatabaseSync2 *r)
1374 p->rng_fault_state = true;
1375 return NT_STATUS_NOT_IMPLEMENTED;
1378 /****************************************************************
1379 ****************************************************************/
1381 NTSTATUS _netr_DatabaseRedo(pipes_struct *p,
1382 struct netr_DatabaseRedo *r)
1384 p->rng_fault_state = true;
1385 return NT_STATUS_NOT_IMPLEMENTED;
1388 /****************************************************************
1389 ****************************************************************/
1391 WERROR _netr_DsRGetDCName(pipes_struct *p,
1392 struct netr_DsRGetDCName *r)
1394 p->rng_fault_state = true;
1395 return WERR_NOT_SUPPORTED;
1398 /****************************************************************
1399 ****************************************************************/
1401 NTSTATUS _netr_LogonGetCapabilities(pipes_struct *p,
1402 struct netr_LogonGetCapabilities *r)
1404 return NT_STATUS_NOT_IMPLEMENTED;
1407 /****************************************************************
1408 ****************************************************************/
1410 WERROR _netr_NETRLOGONSETSERVICEBITS(pipes_struct *p,
1411 struct netr_NETRLOGONSETSERVICEBITS *r)
1413 p->rng_fault_state = true;
1414 return WERR_NOT_SUPPORTED;
1417 /****************************************************************
1418 ****************************************************************/
1420 WERROR _netr_LogonGetTrustRid(pipes_struct *p,
1421 struct netr_LogonGetTrustRid *r)
1423 p->rng_fault_state = true;
1424 return WERR_NOT_SUPPORTED;
1427 /****************************************************************
1428 ****************************************************************/
1430 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(pipes_struct *p,
1431 struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
1433 p->rng_fault_state = true;
1434 return WERR_NOT_SUPPORTED;
1437 /****************************************************************
1438 ****************************************************************/
1440 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(pipes_struct *p,
1441 struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
1443 p->rng_fault_state = true;
1444 return WERR_NOT_SUPPORTED;
1447 /****************************************************************
1448 ****************************************************************/
1450 WERROR _netr_DsRGetDCNameEx(pipes_struct *p,
1451 struct netr_DsRGetDCNameEx *r)
1453 p->rng_fault_state = true;
1454 return WERR_NOT_SUPPORTED;
1457 /****************************************************************
1458 ****************************************************************/
1460 WERROR _netr_DsRGetSiteName(pipes_struct *p,
1461 struct netr_DsRGetSiteName *r)
1463 p->rng_fault_state = true;
1464 return WERR_NOT_SUPPORTED;
1467 /****************************************************************
1468 ****************************************************************/
1470 NTSTATUS _netr_LogonGetDomainInfo(pipes_struct *p,
1471 struct netr_LogonGetDomainInfo *r)
1473 p->rng_fault_state = true;
1474 return NT_STATUS_NOT_IMPLEMENTED;
1477 /****************************************************************
1478 ****************************************************************/
1480 WERROR _netr_ServerPasswordGet(pipes_struct *p,
1481 struct netr_ServerPasswordGet *r)
1483 p->rng_fault_state = true;
1484 return WERR_NOT_SUPPORTED;
1487 /****************************************************************
1488 ****************************************************************/
1490 WERROR _netr_NETRLOGONSENDTOSAM(pipes_struct *p,
1491 struct netr_NETRLOGONSENDTOSAM *r)
1493 p->rng_fault_state = true;
1494 return WERR_NOT_SUPPORTED;
1497 /****************************************************************
1498 ****************************************************************/
1500 WERROR _netr_DsRAddressToSitenamesW(pipes_struct *p,
1501 struct netr_DsRAddressToSitenamesW *r)
1503 p->rng_fault_state = true;
1504 return WERR_NOT_SUPPORTED;
1507 /****************************************************************
1508 ****************************************************************/
1510 WERROR _netr_DsRGetDCNameEx2(pipes_struct *p,
1511 struct netr_DsRGetDCNameEx2 *r)
1513 p->rng_fault_state = true;
1514 return WERR_NOT_SUPPORTED;
1517 /****************************************************************
1518 ****************************************************************/
1520 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(pipes_struct *p,
1521 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
1523 p->rng_fault_state = true;
1524 return WERR_NOT_SUPPORTED;
1527 /****************************************************************
1528 ****************************************************************/
1530 WERROR _netr_NetrEnumerateTrustedDomainsEx(pipes_struct *p,
1531 struct netr_NetrEnumerateTrustedDomainsEx *r)
1533 p->rng_fault_state = true;
1534 return WERR_NOT_SUPPORTED;
1537 /****************************************************************
1538 ****************************************************************/
1540 WERROR _netr_DsRAddressToSitenamesExW(pipes_struct *p,
1541 struct netr_DsRAddressToSitenamesExW *r)
1543 p->rng_fault_state = true;
1544 return WERR_NOT_SUPPORTED;
1547 /****************************************************************
1548 ****************************************************************/
1550 WERROR _netr_DsrGetDcSiteCoverageW(pipes_struct *p,
1551 struct netr_DsrGetDcSiteCoverageW *r)
1553 p->rng_fault_state = true;
1554 return WERR_NOT_SUPPORTED;
1557 /****************************************************************
1558 ****************************************************************/
1560 WERROR _netr_DsrEnumerateDomainTrusts(pipes_struct *p,
1561 struct netr_DsrEnumerateDomainTrusts *r)
1563 p->rng_fault_state = true;
1564 return WERR_NOT_SUPPORTED;
1567 /****************************************************************
1568 ****************************************************************/
1570 WERROR _netr_DsrDeregisterDNSHostRecords(pipes_struct *p,
1571 struct netr_DsrDeregisterDNSHostRecords *r)
1573 p->rng_fault_state = true;
1574 return WERR_NOT_SUPPORTED;
1577 /****************************************************************
1578 ****************************************************************/
1580 NTSTATUS _netr_ServerTrustPasswordsGet(pipes_struct *p,
1581 struct netr_ServerTrustPasswordsGet *r)
1583 p->rng_fault_state = true;
1584 return NT_STATUS_NOT_IMPLEMENTED;
1587 /****************************************************************
1588 ****************************************************************/
1590 WERROR _netr_DsRGetForestTrustInformation(pipes_struct *p,
1591 struct netr_DsRGetForestTrustInformation *r)
1593 p->rng_fault_state = true;
1594 return WERR_NOT_SUPPORTED;
1597 /****************************************************************
1598 ****************************************************************/
1600 WERROR _netr_GetForestTrustInformation(pipes_struct *p,
1601 struct netr_GetForestTrustInformation *r)
1603 p->rng_fault_state = true;
1604 return WERR_NOT_SUPPORTED;
1607 /****************************************************************
1608 ****************************************************************/
1610 NTSTATUS _netr_ServerGetTrustInfo(pipes_struct *p,
1611 struct netr_ServerGetTrustInfo *r)
1613 p->rng_fault_state = true;
1614 return NT_STATUS_NOT_IMPLEMENTED;