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 /****************************************************************
100 ****************************************************************/
102 WERROR _netr_LogonControl2Ex(pipes_struct *p,
103 struct netr_LogonControl2Ex *r)
105 uint32_t flags = 0x0;
106 WERROR pdc_connection_status = WERR_OK;
107 uint32_t logon_attempts = 0x0;
110 const char *dc_name = NULL;
111 struct sockaddr_storage dc_ss;
112 const char *domain = NULL;
113 struct netr_NETLOGON_INFO_1 *info1;
114 struct netr_NETLOGON_INFO_2 *info2;
115 struct netr_NETLOGON_INFO_3 *info3;
118 switch (p->hdr_req.opnum) {
119 case NDR_NETR_LOGONCONTROL:
120 fn = "_netr_LogonControl";
122 case NDR_NETR_LOGONCONTROL2:
123 fn = "_netr_LogonControl2";
125 case NDR_NETR_LOGONCONTROL2EX:
126 fn = "_netr_LogonControl2Ex";
129 return WERR_INVALID_PARAM;
132 tc_status = WERR_NO_SUCH_DOMAIN;
134 switch (r->in.function_code) {
135 case NETLOGON_CONTROL_TC_QUERY:
136 domain = r->in.data->domain;
138 if (!is_trusted_domain(domain)) {
142 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
143 tc_status = WERR_NO_LOGON_SERVERS;
147 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
156 case NETLOGON_CONTROL_REDISCOVER:
157 domain = r->in.data->domain;
159 if (!is_trusted_domain(domain)) {
163 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
164 tc_status = WERR_NO_LOGON_SERVERS;
168 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
178 /* no idea what this should be */
179 DEBUG(0,("%s: unimplemented function level [%d]\n",
180 fn, r->in.function_code));
181 return WERR_UNKNOWN_LEVEL;
184 /* prepare the response */
186 switch (r->in.level) {
188 info1 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_1);
189 W_ERROR_HAVE_NO_MEMORY(info1);
191 info1->flags = flags;
192 info1->pdc_connection_status = pdc_connection_status;
194 r->out.query->info1 = info1;
197 info2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_2);
198 W_ERROR_HAVE_NO_MEMORY(info2);
200 info2->flags = flags;
201 info2->pdc_connection_status = pdc_connection_status;
202 info2->trusted_dc_name = dc_name;
203 info2->tc_connection_status = tc_status;
205 r->out.query->info2 = info2;
208 info3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_3);
209 W_ERROR_HAVE_NO_MEMORY(info3);
211 info3->flags = flags;
212 info3->logon_attempts = logon_attempts;
214 r->out.query->info3 = info3;
217 return WERR_UNKNOWN_LEVEL;
220 if (lp_server_role() == ROLE_DOMAIN_BDC) {
227 /*************************************************************************
228 _netr_NetrEnumerateTrustedDomains
229 *************************************************************************/
231 WERROR _netr_NetrEnumerateTrustedDomains(pipes_struct *p,
232 struct netr_NetrEnumerateTrustedDomains *r)
236 struct trustdom_info **domains;
237 uint32_t num_domains;
238 const char **trusted_domains;
241 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
243 /* set up the Trusted Domain List response */
246 status = pdb_enum_trusteddoms(p->mem_ctx, &num_domains, &domains);
249 if (!NT_STATUS_IS_OK(status)) {
250 return ntstatus_to_werror(status);
253 trusted_domains = talloc_zero_array(p->mem_ctx, const char *, num_domains + 1);
254 if (!trusted_domains) {
258 for (i = 0; i < num_domains; i++) {
259 trusted_domains[i] = talloc_strdup(trusted_domains, domains[i]->name);
260 if (!trusted_domains[i]) {
261 TALLOC_FREE(trusted_domains);
266 if (!push_reg_multi_sz(trusted_domains, &blob, trusted_domains)) {
267 TALLOC_FREE(trusted_domains);
271 r->out.trusted_domains_blob->data = blob.data;
272 r->out.trusted_domains_blob->length = blob.length;
274 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
279 /******************************************************************
280 gets a machine password entry. checks access rights of the host.
281 ******************************************************************/
283 static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
284 enum netr_SchannelType sec_chan_type, struct dom_sid *sid)
286 struct samu *sampass = NULL;
292 char addr[INET6_ADDRSTRLEN];
295 * Currently this code is redundent as we already have a filter
296 * by hostname list. What this code really needs to do is to
297 * get a hosts allowed/hosts denied list from the SAM database
298 * on a per user basis, and make the access decision there.
299 * I will leave this code here for now as a reminder to implement
300 * this at a later date. JRA.
303 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
304 client_name(get_client_fd()),
305 client_addr(get_client_fd(),addr,sizeof(addr)))) {
306 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
311 if ( !(sampass = samu_new( NULL )) ) {
312 return NT_STATUS_NO_MEMORY;
315 /* JRA. This is ok as it is only used for generating the challenge. */
317 ret = pdb_getsampwnam(sampass, mach_acct);
321 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
322 TALLOC_FREE(sampass);
323 return NT_STATUS_ACCESS_DENIED;
326 acct_ctrl = pdb_get_acct_ctrl(sampass);
327 if (acct_ctrl & ACB_DISABLED) {
328 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
329 TALLOC_FREE(sampass);
330 return NT_STATUS_ACCOUNT_DISABLED;
333 if (!(acct_ctrl & ACB_SVRTRUST) &&
334 !(acct_ctrl & ACB_WSTRUST) &&
335 !(acct_ctrl & ACB_DOMTRUST))
337 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
338 TALLOC_FREE(sampass);
339 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
342 switch (sec_chan_type) {
344 if (!(acct_ctrl & ACB_SVRTRUST)) {
345 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
346 "but not a server trust account\n", mach_acct));
347 TALLOC_FREE(sampass);
348 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
352 if (!(acct_ctrl & ACB_WSTRUST)) {
353 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
354 "but not a workstation trust account\n", mach_acct));
355 TALLOC_FREE(sampass);
356 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
359 case SEC_CHAN_DOMAIN:
360 if (!(acct_ctrl & ACB_DOMTRUST)) {
361 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
362 "but not a interdomain trust account\n", mach_acct));
363 TALLOC_FREE(sampass);
364 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
371 if ((pass = pdb_get_nt_passwd(sampass)) == NULL) {
372 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
373 TALLOC_FREE(sampass);
374 return NT_STATUS_LOGON_FAILURE;
377 memcpy(md4pw->hash, pass, 16);
378 dump_data(5, md4pw->hash, 16);
380 sid_copy(sid, pdb_get_user_sid(sampass));
382 TALLOC_FREE(sampass);
389 /*************************************************************************
390 _netr_ServerReqChallenge
391 *************************************************************************/
393 NTSTATUS _netr_ServerReqChallenge(pipes_struct *p,
394 struct netr_ServerReqChallenge *r)
396 struct netlogon_server_pipe_state *pipe_state =
397 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
400 DEBUG(10,("_netr_ServerReqChallenge: new challenge requested. Clearing old state.\n"));
401 talloc_free(pipe_state);
402 p->private_data = NULL;
405 pipe_state = talloc(p, struct netlogon_server_pipe_state);
406 NT_STATUS_HAVE_NO_MEMORY(pipe_state);
408 pipe_state->client_challenge = *r->in.credentials;
410 generate_random_buffer(pipe_state->server_challenge.data,
411 sizeof(pipe_state->server_challenge.data));
413 *r->out.return_credentials = pipe_state->server_challenge;
415 p->private_data = pipe_state;
420 /*************************************************************************
421 _netr_ServerAuthenticate
422 Create the initial credentials.
423 *************************************************************************/
425 NTSTATUS _netr_ServerAuthenticate(pipes_struct *p,
426 struct netr_ServerAuthenticate *r)
428 struct netr_ServerAuthenticate3 a;
429 uint32_t negotiate_flags = 0;
432 a.in.server_name = r->in.server_name;
433 a.in.account_name = r->in.account_name;
434 a.in.secure_channel_type = r->in.secure_channel_type;
435 a.in.computer_name = r->in.computer_name;
436 a.in.credentials = r->in.credentials;
437 a.in.negotiate_flags = &negotiate_flags;
439 a.out.return_credentials = r->out.return_credentials;
441 a.out.negotiate_flags = &negotiate_flags;
443 return _netr_ServerAuthenticate3(p, &a);
447 /*************************************************************************
448 _netr_ServerAuthenticate3
449 *************************************************************************/
451 NTSTATUS _netr_ServerAuthenticate3(pipes_struct *p,
452 struct netr_ServerAuthenticate3 *r)
456 /* r->in.negotiate_flags is an aliased pointer to r->out.negotiate_flags,
457 * so use a copy to avoid destroying the client values. */
458 uint32_t in_neg_flags = *r->in.negotiate_flags;
461 struct samr_Password mach_pwd;
462 struct netlogon_creds_CredentialState *creds;
463 struct netlogon_server_pipe_state *pipe_state =
464 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
466 /* According to Microsoft (see bugid #6099)
467 * Windows 7 looks at the negotiate_flags
468 * returned in this structure *even if the
469 * call fails with access denied* ! So in order
470 * to allow Win7 to connect to a Samba NT style
471 * PDC we set the flags before we know if it's
476 srv_flgs = NETLOGON_NEG_ACCOUNT_LOCKOUT |
477 NETLOGON_NEG_PERSISTENT_SAMREPL |
478 NETLOGON_NEG_ARCFOUR |
479 NETLOGON_NEG_PROMOTION_COUNT |
480 NETLOGON_NEG_CHANGELOG_BDC |
481 NETLOGON_NEG_FULL_SYNC_REPL |
482 NETLOGON_NEG_MULTIPLE_SIDS |
484 NETLOGON_NEG_PASSWORD_CHANGE_REFUSAL |
485 NETLOGON_NEG_PASSWORD_SET2;
487 /* Ensure we support strong (128-bit) keys. */
488 if (in_neg_flags & NETLOGON_NEG_STRONG_KEYS) {
489 srv_flgs |= NETLOGON_NEG_STRONG_KEYS;
492 if (lp_server_schannel() != false) {
493 srv_flgs |= NETLOGON_NEG_SCHANNEL;
496 switch (p->hdr_req.opnum) {
497 case NDR_NETR_SERVERAUTHENTICATE:
498 fn = "_netr_ServerAuthenticate";
500 case NDR_NETR_SERVERAUTHENTICATE2:
501 fn = "_netr_ServerAuthenticate2";
503 case NDR_NETR_SERVERAUTHENTICATE3:
504 fn = "_netr_ServerAuthenticate3";
507 return NT_STATUS_INTERNAL_ERROR;
510 /* We use this as the key to store the creds: */
511 /* r->in.computer_name */
514 DEBUG(0,("%s: no challenge sent to client %s\n", fn,
515 r->in.computer_name));
516 status = NT_STATUS_ACCESS_DENIED;
520 if ( (lp_server_schannel() == true) &&
521 ((in_neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
523 /* schannel must be used, but client did not offer it. */
524 DEBUG(0,("%s: schannel required but client failed "
525 "to offer it. Client was %s\n",
526 fn, r->in.account_name));
527 status = NT_STATUS_ACCESS_DENIED;
531 status = get_md4pw(&mach_pwd,
533 r->in.secure_channel_type,
535 if (!NT_STATUS_IS_OK(status)) {
536 DEBUG(0,("%s: failed to get machine password for "
538 fn, r->in.account_name, nt_errstr(status) ));
539 /* always return NT_STATUS_ACCESS_DENIED */
540 status = NT_STATUS_ACCESS_DENIED;
544 /* From the client / server challenges and md4 password, generate sess key */
545 /* Check client credentials are valid. */
546 creds = netlogon_creds_server_init(p->mem_ctx,
549 r->in.secure_channel_type,
550 &pipe_state->client_challenge,
551 &pipe_state->server_challenge,
554 r->out.return_credentials,
555 *r->in.negotiate_flags);
557 DEBUG(0,("%s: netlogon_creds_server_check failed. Rejecting auth "
558 "request from client %s machine account %s\n",
559 fn, r->in.computer_name,
560 r->in.account_name));
561 status = NT_STATUS_ACCESS_DENIED;
565 creds->sid = sid_dup_talloc(creds, &sid);
567 status = NT_STATUS_NO_MEMORY;
571 /* Store off the state so we can continue after client disconnect. */
573 status = schannel_store_session_key(p->mem_ctx, creds);
576 if (!NT_STATUS_IS_OK(status)) {
580 sid_peek_rid(&sid, r->out.rid);
582 status = NT_STATUS_OK;
586 *r->out.negotiate_flags = srv_flgs;
590 /*************************************************************************
591 _netr_ServerAuthenticate2
592 *************************************************************************/
594 NTSTATUS _netr_ServerAuthenticate2(pipes_struct *p,
595 struct netr_ServerAuthenticate2 *r)
597 struct netr_ServerAuthenticate3 a;
600 a.in.server_name = r->in.server_name;
601 a.in.account_name = r->in.account_name;
602 a.in.secure_channel_type = r->in.secure_channel_type;
603 a.in.computer_name = r->in.computer_name;
604 a.in.credentials = r->in.credentials;
605 a.in.negotiate_flags = r->in.negotiate_flags;
607 a.out.return_credentials = r->out.return_credentials;
609 a.out.negotiate_flags = r->out.negotiate_flags;
611 return _netr_ServerAuthenticate3(p, &a);
614 /*************************************************************************
615 *************************************************************************/
617 static NTSTATUS netr_creds_server_step_check(pipes_struct *p,
619 const char *computer_name,
620 struct netr_Authenticator *received_authenticator,
621 struct netr_Authenticator *return_authenticator,
622 struct netlogon_creds_CredentialState **creds_out)
625 struct tdb_context *tdb;
626 bool schannel_global_required = (lp_server_schannel() == true) ? true:false;
627 bool schannel_in_use = (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL) ? true:false; /* &&
628 (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY ||
629 p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY); */
631 tdb = open_schannel_session_store(mem_ctx);
633 return NT_STATUS_ACCESS_DENIED;
636 status = schannel_creds_server_step_check_tdb(tdb, mem_ctx,
638 schannel_global_required,
640 received_authenticator,
641 return_authenticator,
648 /*************************************************************************
649 *************************************************************************/
651 static NTSTATUS netr_find_machine_account(TALLOC_CTX *mem_ctx,
652 const char *account_name,
653 struct samu **sampassp)
655 struct samu *sampass;
659 sampass = samu_new(mem_ctx);
661 return NT_STATUS_NO_MEMORY;
665 ret = pdb_getsampwnam(sampass, account_name);
669 TALLOC_FREE(sampass);
670 return NT_STATUS_ACCESS_DENIED;
673 /* Ensure the account exists and is a machine account. */
675 acct_ctrl = pdb_get_acct_ctrl(sampass);
677 if (!(acct_ctrl & ACB_WSTRUST ||
678 acct_ctrl & ACB_SVRTRUST ||
679 acct_ctrl & ACB_DOMTRUST)) {
680 TALLOC_FREE(sampass);
681 return NT_STATUS_NO_SUCH_USER;
684 if (acct_ctrl & ACB_DISABLED) {
685 TALLOC_FREE(sampass);
686 return NT_STATUS_ACCOUNT_DISABLED;
694 /*************************************************************************
695 *************************************************************************/
697 static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
698 struct samu *sampass,
699 DATA_BLOB *plaintext_blob,
700 struct samr_Password *nt_hash,
701 struct samr_Password *lm_hash)
705 const char *plaintext = NULL;
706 size_t plaintext_len;
707 struct samr_Password nt_hash_local;
710 return NT_STATUS_INVALID_PARAMETER;
713 if (plaintext_blob) {
714 if (!convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
715 plaintext_blob->data, plaintext_blob->length,
716 &plaintext, &plaintext_len, false))
719 mdfour(nt_hash_local.hash, plaintext_blob->data, plaintext_blob->length);
720 nt_hash = &nt_hash_local;
725 if (!pdb_set_plaintext_passwd(sampass, plaintext)) {
726 return NT_STATUS_ACCESS_DENIED;
733 old_pw = pdb_get_nt_passwd(sampass);
735 if (old_pw && memcmp(nt_hash->hash, old_pw, 16) == 0) {
736 /* Avoid backend modificiations and other fun if the
737 client changed the password to the *same thing* */
739 /* LM password should be NULL for machines */
740 if (!pdb_set_lanman_passwd(sampass, NULL, PDB_CHANGED)) {
741 return NT_STATUS_NO_MEMORY;
743 if (!pdb_set_nt_passwd(sampass, nt_hash->hash, PDB_CHANGED)) {
744 return NT_STATUS_NO_MEMORY;
747 if (!pdb_set_pass_last_set_time(sampass, time(NULL), PDB_CHANGED)) {
748 /* Not quite sure what this one qualifies as, but this will do */
749 return NT_STATUS_UNSUCCESSFUL;
756 status = pdb_update_sam_account(sampass);
762 /*************************************************************************
763 _netr_ServerPasswordSet
764 *************************************************************************/
766 NTSTATUS _netr_ServerPasswordSet(pipes_struct *p,
767 struct netr_ServerPasswordSet *r)
769 NTSTATUS status = NT_STATUS_OK;
770 struct samu *sampass=NULL;
772 struct netlogon_creds_CredentialState *creds;
774 DEBUG(5,("_netr_ServerPasswordSet: %d\n", __LINE__));
777 status = netr_creds_server_step_check(p, p->mem_ctx,
780 r->out.return_authenticator,
784 if (!NT_STATUS_IS_OK(status)) {
785 DEBUG(2,("_netr_ServerPasswordSet: netlogon_creds_server_step failed. Rejecting auth "
786 "request from client %s machine account %s\n",
787 r->in.computer_name, creds->computer_name));
792 DEBUG(3,("_netr_ServerPasswordSet: Server Password Set by remote machine:[%s] on account [%s]\n",
793 r->in.computer_name, creds->computer_name));
795 netlogon_creds_des_decrypt(creds, r->in.new_password);
797 DEBUG(100,("_netr_ServerPasswordSet: new given value was :\n"));
798 for(i = 0; i < sizeof(r->in.new_password->hash); i++)
799 DEBUG(100,("%02X ", r->in.new_password->hash[i]));
802 status = netr_find_machine_account(p->mem_ctx,
805 if (!NT_STATUS_IS_OK(status)) {
809 status = netr_set_machine_account_password(sampass,
814 TALLOC_FREE(sampass);
818 /****************************************************************
819 _netr_ServerPasswordSet2
820 ****************************************************************/
822 NTSTATUS _netr_ServerPasswordSet2(pipes_struct *p,
823 struct netr_ServerPasswordSet2 *r)
826 struct netlogon_creds_CredentialState *creds;
827 struct samu *sampass;
829 struct samr_CryptPassword password_buf;
830 struct samr_Password nt_hash;
833 status = netr_creds_server_step_check(p, p->mem_ctx,
836 r->out.return_authenticator,
840 if (!NT_STATUS_IS_OK(status)) {
841 DEBUG(2,("_netr_ServerPasswordSet2: netlogon_creds_server_step "
842 "failed. Rejecting auth request from client %s machine account %s\n",
843 r->in.computer_name, creds->computer_name));
848 memcpy(password_buf.data, r->in.new_password->data, 512);
849 SIVAL(password_buf.data, 512, r->in.new_password->length);
850 netlogon_creds_arcfour_crypt(creds, password_buf.data, 516);
852 if (!extract_pw_from_buffer(p->mem_ctx, password_buf.data, &plaintext)) {
853 return NT_STATUS_WRONG_PASSWORD;
856 mdfour(nt_hash.hash, plaintext.data, plaintext.length);
858 status = netr_find_machine_account(p->mem_ctx,
861 if (!NT_STATUS_IS_OK(status)) {
865 status = netr_set_machine_account_password(sampass,
870 TALLOC_FREE(sampass);
874 /*************************************************************************
876 *************************************************************************/
878 NTSTATUS _netr_LogonSamLogoff(pipes_struct *p,
879 struct netr_LogonSamLogoff *r)
882 struct netlogon_creds_CredentialState *creds;
885 status = netr_creds_server_step_check(p, p->mem_ctx,
888 r->out.return_authenticator,
895 /*************************************************************************
896 _netr_LogonSamLogon_base
897 *************************************************************************/
899 static NTSTATUS _netr_LogonSamLogon_base(pipes_struct *p,
900 struct netr_LogonSamLogonEx *r,
901 struct netlogon_creds_CredentialState *creds)
903 NTSTATUS status = NT_STATUS_OK;
904 union netr_LogonLevel *logon = r->in.logon;
905 const char *nt_username, *nt_domain, *nt_workstation;
906 auth_usersupplied_info *user_info = NULL;
907 auth_serversupplied_info *server_info = NULL;
908 struct auth_context *auth_context = NULL;
909 uint8_t pipe_session_key[16];
910 bool process_creds = true;
913 switch (p->hdr_req.opnum) {
914 case NDR_NETR_LOGONSAMLOGON:
915 process_creds = true;
916 fn = "_netr_LogonSamLogon";
918 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
919 process_creds = true;
920 fn = "_netr_LogonSamLogonWithFlags";
922 case NDR_NETR_LOGONSAMLOGONEX:
923 process_creds = false;
924 fn = "_netr_LogonSamLogonEx";
927 return NT_STATUS_INTERNAL_ERROR;
930 *r->out.authoritative = true; /* authoritative response */
932 switch (r->in.validation_level) {
934 r->out.validation->sam2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo2);
935 if (!r->out.validation->sam2) {
936 return NT_STATUS_NO_MEMORY;
940 r->out.validation->sam3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo3);
941 if (!r->out.validation->sam3) {
942 return NT_STATUS_NO_MEMORY;
946 r->out.validation->sam6 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo6);
947 if (!r->out.validation->sam6) {
948 return NT_STATUS_NO_MEMORY;
952 DEBUG(0,("%s: bad validation_level value %d.\n",
953 fn, (int)r->in.validation_level));
954 return NT_STATUS_INVALID_INFO_CLASS;
957 switch (r->in.logon_level) {
958 case NetlogonInteractiveInformation:
959 case NetlogonServiceInformation:
960 case NetlogonInteractiveTransitiveInformation:
961 case NetlogonServiceTransitiveInformation:
962 nt_username = logon->password->identity_info.account_name.string;
963 nt_domain = logon->password->identity_info.domain_name.string;
964 nt_workstation = logon->password->identity_info.workstation.string;
966 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
968 case NetlogonNetworkInformation:
969 case NetlogonNetworkTransitiveInformation:
970 nt_username = logon->network->identity_info.account_name.string;
971 nt_domain = logon->network->identity_info.domain_name.string;
972 nt_workstation = logon->network->identity_info.workstation.string;
974 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
977 DEBUG(2,("SAM Logon: unsupported switch value\n"));
978 return NT_STATUS_INVALID_INFO_CLASS;
981 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
982 fstrcpy(current_user_info.smb_name, nt_username);
983 sub_set_smb_name(nt_username);
985 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
986 r->in.validation_level, nt_username));
988 status = NT_STATUS_OK;
990 switch (r->in.logon_level) {
991 case NetlogonNetworkInformation:
992 case NetlogonNetworkTransitiveInformation:
994 const char *wksname = nt_workstation;
996 status = make_auth_context_fixed(&auth_context,
997 logon->network->challenge);
998 if (!NT_STATUS_IS_OK(status)) {
1002 /* For a network logon, the workstation name comes in with two
1003 * backslashes in the front. Strip them if they are there. */
1005 if (*wksname == '\\') wksname++;
1006 if (*wksname == '\\') wksname++;
1008 /* Standard challenge/response authenticaion */
1009 if (!make_user_info_netlogon_network(&user_info,
1010 nt_username, nt_domain,
1012 logon->network->identity_info.parameter_control,
1013 logon->network->lm.data,
1014 logon->network->lm.length,
1015 logon->network->nt.data,
1016 logon->network->nt.length)) {
1017 status = NT_STATUS_NO_MEMORY;
1021 case NetlogonInteractiveInformation:
1022 case NetlogonServiceInformation:
1023 case NetlogonInteractiveTransitiveInformation:
1024 case NetlogonServiceTransitiveInformation:
1026 /* 'Interactive' authentication, supplies the password in its
1027 MD4 form, encrypted with the session key. We will convert
1028 this to challenge/response for the auth subsystem to chew
1033 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
1037 auth_context->get_ntlm_challenge(auth_context, chal);
1039 if (!make_user_info_netlogon_interactive(&user_info,
1040 nt_username, nt_domain,
1042 logon->password->identity_info.parameter_control,
1044 logon->password->lmpassword.hash,
1045 logon->password->ntpassword.hash,
1046 creds->session_key)) {
1047 status = NT_STATUS_NO_MEMORY;
1052 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1053 return NT_STATUS_INVALID_INFO_CLASS;
1056 if ( NT_STATUS_IS_OK(status) ) {
1057 status = auth_context->check_ntlm_password(auth_context,
1058 user_info, &server_info);
1061 (auth_context->free)(&auth_context);
1062 free_user_info(&user_info);
1064 DEBUG(5,("%s: check_password returned status %s\n",
1065 fn, nt_errstr(status)));
1067 /* Check account and password */
1069 if (!NT_STATUS_IS_OK(status)) {
1070 /* If we don't know what this domain is, we need to
1071 indicate that we are not authoritative. This
1072 allows the client to decide if it needs to try
1073 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
1074 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1075 && !strequal(nt_domain, get_global_sam_name())
1076 && !is_trusted_domain(nt_domain) )
1077 *r->out.authoritative = false; /* We are not authoritative */
1079 TALLOC_FREE(server_info);
1083 if (server_info->guest) {
1084 /* We don't like guest domain logons... */
1085 DEBUG(5,("%s: Attempted domain logon as GUEST "
1087 TALLOC_FREE(server_info);
1088 return NT_STATUS_LOGON_FAILURE;
1091 /* This is the point at which, if the login was successful, that
1092 the SAM Local Security Authority should record that the user is
1093 logged in to the domain. */
1095 if (process_creds) {
1096 /* Get the pipe session key from the creds. */
1097 memcpy(pipe_session_key, creds->session_key, 16);
1099 /* Get the pipe session key from the schannel. */
1100 if ((p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL)
1101 || (p->auth.a_u.schannel_auth == NULL)) {
1102 return NT_STATUS_INVALID_HANDLE;
1104 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->creds->session_key, 16);
1107 switch (r->in.validation_level) {
1109 status = serverinfo_to_SamInfo2(server_info, pipe_session_key, 16,
1110 r->out.validation->sam2);
1113 status = serverinfo_to_SamInfo3(server_info, pipe_session_key, 16,
1114 r->out.validation->sam3);
1117 status = serverinfo_to_SamInfo6(server_info, pipe_session_key, 16,
1118 r->out.validation->sam6);
1122 TALLOC_FREE(server_info);
1127 /****************************************************************
1128 _netr_LogonSamLogonWithFlags
1129 ****************************************************************/
1131 NTSTATUS _netr_LogonSamLogonWithFlags(pipes_struct *p,
1132 struct netr_LogonSamLogonWithFlags *r)
1135 struct netlogon_creds_CredentialState *creds;
1136 struct netr_LogonSamLogonEx r2;
1137 struct netr_Authenticator return_authenticator;
1140 status = netr_creds_server_step_check(p, p->mem_ctx,
1141 r->in.computer_name,
1143 &return_authenticator,
1146 if (!NT_STATUS_IS_OK(status)) {
1150 r2.in.server_name = r->in.server_name;
1151 r2.in.computer_name = r->in.computer_name;
1152 r2.in.logon_level = r->in.logon_level;
1153 r2.in.logon = r->in.logon;
1154 r2.in.validation_level = r->in.validation_level;
1155 r2.in.flags = r->in.flags;
1156 r2.out.validation = r->out.validation;
1157 r2.out.authoritative = r->out.authoritative;
1158 r2.out.flags = r->out.flags;
1160 status = _netr_LogonSamLogon_base(p, &r2, creds);
1162 *r->out.return_authenticator = return_authenticator;
1167 /*************************************************************************
1169 *************************************************************************/
1171 NTSTATUS _netr_LogonSamLogon(pipes_struct *p,
1172 struct netr_LogonSamLogon *r)
1175 struct netr_LogonSamLogonWithFlags r2;
1178 r2.in.server_name = r->in.server_name;
1179 r2.in.computer_name = r->in.computer_name;
1180 r2.in.credential = r->in.credential;
1181 r2.in.logon_level = r->in.logon_level;
1182 r2.in.logon = r->in.logon;
1183 r2.in.validation_level = r->in.validation_level;
1184 r2.in.return_authenticator = r->in.return_authenticator;
1185 r2.in.flags = &flags;
1186 r2.out.validation = r->out.validation;
1187 r2.out.authoritative = r->out.authoritative;
1188 r2.out.flags = &flags;
1189 r2.out.return_authenticator = r->out.return_authenticator;
1191 status = _netr_LogonSamLogonWithFlags(p, &r2);
1196 /*************************************************************************
1197 _netr_LogonSamLogonEx
1198 - no credential chaining. Map into net sam logon.
1199 *************************************************************************/
1201 NTSTATUS _netr_LogonSamLogonEx(pipes_struct *p,
1202 struct netr_LogonSamLogonEx *r)
1205 struct netlogon_creds_CredentialState *creds = NULL;
1208 status = schannel_fetch_session_key(p->mem_ctx, r->in.computer_name, &creds);
1210 if (!NT_STATUS_IS_OK(status)) {
1214 /* Only allow this if the pipe is protected. */
1215 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1216 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1217 get_remote_machine_name() ));
1218 return NT_STATUS_INVALID_PARAMETER;
1221 status = _netr_LogonSamLogon_base(p, r, creds);
1227 /*************************************************************************
1229 *************************************************************************/
1230 #if 0 /* JERRY -- not correct */
1231 NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1232 DS_R_ENUM_DOM_TRUSTS *r_u)
1234 NTSTATUS status = NT_STATUS_OK;
1236 /* TODO: According to MSDN, the can only be executed against a
1237 DC or domain member running Windows 2000 or later. Need
1238 to test against a standalone 2k server and see what it
1239 does. A windows 2000 DC includes its own domain in the
1247 /****************************************************************
1248 ****************************************************************/
1250 WERROR _netr_LogonUasLogon(pipes_struct *p,
1251 struct netr_LogonUasLogon *r)
1253 p->rng_fault_state = true;
1254 return WERR_NOT_SUPPORTED;
1257 /****************************************************************
1258 ****************************************************************/
1260 WERROR _netr_LogonUasLogoff(pipes_struct *p,
1261 struct netr_LogonUasLogoff *r)
1263 p->rng_fault_state = true;
1264 return WERR_NOT_SUPPORTED;
1267 /****************************************************************
1268 ****************************************************************/
1270 NTSTATUS _netr_DatabaseDeltas(pipes_struct *p,
1271 struct netr_DatabaseDeltas *r)
1273 p->rng_fault_state = true;
1274 return NT_STATUS_NOT_IMPLEMENTED;
1277 /****************************************************************
1278 ****************************************************************/
1280 NTSTATUS _netr_DatabaseSync(pipes_struct *p,
1281 struct netr_DatabaseSync *r)
1283 p->rng_fault_state = true;
1284 return NT_STATUS_NOT_IMPLEMENTED;
1287 /****************************************************************
1288 ****************************************************************/
1290 NTSTATUS _netr_AccountDeltas(pipes_struct *p,
1291 struct netr_AccountDeltas *r)
1293 p->rng_fault_state = true;
1294 return NT_STATUS_NOT_IMPLEMENTED;
1297 /****************************************************************
1298 ****************************************************************/
1300 NTSTATUS _netr_AccountSync(pipes_struct *p,
1301 struct netr_AccountSync *r)
1303 p->rng_fault_state = true;
1304 return NT_STATUS_NOT_IMPLEMENTED;
1307 /****************************************************************
1308 ****************************************************************/
1310 WERROR _netr_GetDcName(pipes_struct *p,
1311 struct netr_GetDcName *r)
1313 p->rng_fault_state = true;
1314 return WERR_NOT_SUPPORTED;
1317 /****************************************************************
1318 ****************************************************************/
1320 WERROR _netr_GetAnyDCName(pipes_struct *p,
1321 struct netr_GetAnyDCName *r)
1323 p->rng_fault_state = true;
1324 return WERR_NOT_SUPPORTED;
1327 /****************************************************************
1328 ****************************************************************/
1330 NTSTATUS _netr_DatabaseSync2(pipes_struct *p,
1331 struct netr_DatabaseSync2 *r)
1333 p->rng_fault_state = true;
1334 return NT_STATUS_NOT_IMPLEMENTED;
1337 /****************************************************************
1338 ****************************************************************/
1340 NTSTATUS _netr_DatabaseRedo(pipes_struct *p,
1341 struct netr_DatabaseRedo *r)
1343 p->rng_fault_state = true;
1344 return NT_STATUS_NOT_IMPLEMENTED;
1347 /****************************************************************
1348 ****************************************************************/
1350 WERROR _netr_DsRGetDCName(pipes_struct *p,
1351 struct netr_DsRGetDCName *r)
1353 p->rng_fault_state = true;
1354 return WERR_NOT_SUPPORTED;
1357 /****************************************************************
1358 ****************************************************************/
1360 NTSTATUS _netr_LogonGetCapabilities(pipes_struct *p,
1361 struct netr_LogonGetCapabilities *r)
1363 return NT_STATUS_NOT_IMPLEMENTED;
1366 /****************************************************************
1367 ****************************************************************/
1369 WERROR _netr_NETRLOGONSETSERVICEBITS(pipes_struct *p,
1370 struct netr_NETRLOGONSETSERVICEBITS *r)
1372 p->rng_fault_state = true;
1373 return WERR_NOT_SUPPORTED;
1376 /****************************************************************
1377 ****************************************************************/
1379 WERROR _netr_LogonGetTrustRid(pipes_struct *p,
1380 struct netr_LogonGetTrustRid *r)
1382 p->rng_fault_state = true;
1383 return WERR_NOT_SUPPORTED;
1386 /****************************************************************
1387 ****************************************************************/
1389 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(pipes_struct *p,
1390 struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
1392 p->rng_fault_state = true;
1393 return WERR_NOT_SUPPORTED;
1396 /****************************************************************
1397 ****************************************************************/
1399 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(pipes_struct *p,
1400 struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
1402 p->rng_fault_state = true;
1403 return WERR_NOT_SUPPORTED;
1406 /****************************************************************
1407 ****************************************************************/
1409 WERROR _netr_DsRGetDCNameEx(pipes_struct *p,
1410 struct netr_DsRGetDCNameEx *r)
1412 p->rng_fault_state = true;
1413 return WERR_NOT_SUPPORTED;
1416 /****************************************************************
1417 ****************************************************************/
1419 WERROR _netr_DsRGetSiteName(pipes_struct *p,
1420 struct netr_DsRGetSiteName *r)
1422 p->rng_fault_state = true;
1423 return WERR_NOT_SUPPORTED;
1426 /****************************************************************
1427 ****************************************************************/
1429 NTSTATUS _netr_LogonGetDomainInfo(pipes_struct *p,
1430 struct netr_LogonGetDomainInfo *r)
1432 p->rng_fault_state = true;
1433 return NT_STATUS_NOT_IMPLEMENTED;
1436 /****************************************************************
1437 ****************************************************************/
1439 WERROR _netr_ServerPasswordGet(pipes_struct *p,
1440 struct netr_ServerPasswordGet *r)
1442 p->rng_fault_state = true;
1443 return WERR_NOT_SUPPORTED;
1446 /****************************************************************
1447 ****************************************************************/
1449 WERROR _netr_NETRLOGONSENDTOSAM(pipes_struct *p,
1450 struct netr_NETRLOGONSENDTOSAM *r)
1452 p->rng_fault_state = true;
1453 return WERR_NOT_SUPPORTED;
1456 /****************************************************************
1457 ****************************************************************/
1459 WERROR _netr_DsRAddressToSitenamesW(pipes_struct *p,
1460 struct netr_DsRAddressToSitenamesW *r)
1462 p->rng_fault_state = true;
1463 return WERR_NOT_SUPPORTED;
1466 /****************************************************************
1467 ****************************************************************/
1469 WERROR _netr_DsRGetDCNameEx2(pipes_struct *p,
1470 struct netr_DsRGetDCNameEx2 *r)
1472 p->rng_fault_state = true;
1473 return WERR_NOT_SUPPORTED;
1476 /****************************************************************
1477 ****************************************************************/
1479 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(pipes_struct *p,
1480 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
1482 p->rng_fault_state = true;
1483 return WERR_NOT_SUPPORTED;
1486 /****************************************************************
1487 ****************************************************************/
1489 WERROR _netr_NetrEnumerateTrustedDomainsEx(pipes_struct *p,
1490 struct netr_NetrEnumerateTrustedDomainsEx *r)
1492 p->rng_fault_state = true;
1493 return WERR_NOT_SUPPORTED;
1496 /****************************************************************
1497 ****************************************************************/
1499 WERROR _netr_DsRAddressToSitenamesExW(pipes_struct *p,
1500 struct netr_DsRAddressToSitenamesExW *r)
1502 p->rng_fault_state = true;
1503 return WERR_NOT_SUPPORTED;
1506 /****************************************************************
1507 ****************************************************************/
1509 WERROR _netr_DsrGetDcSiteCoverageW(pipes_struct *p,
1510 struct netr_DsrGetDcSiteCoverageW *r)
1512 p->rng_fault_state = true;
1513 return WERR_NOT_SUPPORTED;
1516 /****************************************************************
1517 ****************************************************************/
1519 WERROR _netr_DsrEnumerateDomainTrusts(pipes_struct *p,
1520 struct netr_DsrEnumerateDomainTrusts *r)
1522 p->rng_fault_state = true;
1523 return WERR_NOT_SUPPORTED;
1526 /****************************************************************
1527 ****************************************************************/
1529 WERROR _netr_DsrDeregisterDNSHostRecords(pipes_struct *p,
1530 struct netr_DsrDeregisterDNSHostRecords *r)
1532 p->rng_fault_state = true;
1533 return WERR_NOT_SUPPORTED;
1536 /****************************************************************
1537 ****************************************************************/
1539 NTSTATUS _netr_ServerTrustPasswordsGet(pipes_struct *p,
1540 struct netr_ServerTrustPasswordsGet *r)
1542 p->rng_fault_state = true;
1543 return NT_STATUS_NOT_IMPLEMENTED;
1546 /****************************************************************
1547 ****************************************************************/
1549 WERROR _netr_DsRGetForestTrustInformation(pipes_struct *p,
1550 struct netr_DsRGetForestTrustInformation *r)
1552 p->rng_fault_state = true;
1553 return WERR_NOT_SUPPORTED;
1556 /****************************************************************
1557 ****************************************************************/
1559 WERROR _netr_GetForestTrustInformation(pipes_struct *p,
1560 struct netr_GetForestTrustInformation *r)
1562 p->rng_fault_state = true;
1563 return WERR_NOT_SUPPORTED;
1566 /****************************************************************
1567 ****************************************************************/
1569 NTSTATUS _netr_ServerGetTrustInfo(pipes_struct *p,
1570 struct netr_ServerGetTrustInfo *r)
1572 p->rng_fault_state = true;
1573 return NT_STATUS_NOT_IMPLEMENTED;