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"
31 extern userdom_struct current_user_info;
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 /*************************************************************************
43 *************************************************************************/
45 WERROR _netr_LogonControl(pipes_struct *p,
46 struct netr_LogonControl *r)
48 struct netr_LogonControl2Ex l;
50 switch (r->in.level) {
54 return WERR_NOT_SUPPORTED;
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;
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,
79 /*************************************************************************
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 /****************************************************************
99 ****************************************************************/
101 WERROR _netr_LogonControl2Ex(pipes_struct *p,
102 struct netr_LogonControl2Ex *r)
104 uint32_t flags = 0x0;
105 WERROR pdc_connection_status = WERR_OK;
106 uint32_t logon_attempts = 0x0;
109 const char *dc_name = NULL;
110 struct sockaddr_storage dc_ss;
111 const char *domain = NULL;
112 struct netr_NETLOGON_INFO_1 *info1;
113 struct netr_NETLOGON_INFO_2 *info2;
114 struct netr_NETLOGON_INFO_3 *info3;
117 switch (p->hdr_req.opnum) {
118 case NDR_NETR_LOGONCONTROL:
119 fn = "_netr_LogonControl";
121 case NDR_NETR_LOGONCONTROL2:
122 fn = "_netr_LogonControl2";
124 case NDR_NETR_LOGONCONTROL2EX:
125 fn = "_netr_LogonControl2Ex";
128 return WERR_INVALID_PARAM;
131 tc_status = WERR_NO_SUCH_DOMAIN;
133 switch (r->in.function_code) {
134 case NETLOGON_CONTROL_TC_QUERY:
135 domain = r->in.data->domain;
137 if ( !is_trusted_domain( domain ) )
140 if ( !get_dc_name( domain, NULL, dc_name2, &dc_ss ) ) {
141 tc_status = WERR_NO_LOGON_SERVERS;
145 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
154 case NETLOGON_CONTROL_REDISCOVER:
155 domain = r->in.data->domain;
157 if ( !is_trusted_domain( domain ) )
160 if ( !get_dc_name( domain, NULL, dc_name2, &dc_ss ) ) {
161 tc_status = WERR_NO_LOGON_SERVERS;
165 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
175 /* no idea what this should be */
176 DEBUG(0,("%s: unimplemented function level [%d]\n",
177 fn, r->in.function_code));
178 return WERR_UNKNOWN_LEVEL;
181 /* prepare the response */
183 switch (r->in.level) {
185 info1 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_1);
186 W_ERROR_HAVE_NO_MEMORY(info1);
188 info1->flags = flags;
189 info1->pdc_connection_status = pdc_connection_status;
191 r->out.query->info1 = info1;
194 info2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_2);
195 W_ERROR_HAVE_NO_MEMORY(info2);
197 info2->flags = flags;
198 info2->pdc_connection_status = pdc_connection_status;
199 info2->trusted_dc_name = dc_name;
200 info2->tc_connection_status = tc_status;
202 r->out.query->info2 = info2;
205 info3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_3);
206 W_ERROR_HAVE_NO_MEMORY(info3);
208 info3->flags = flags;
209 info3->logon_attempts = logon_attempts;
211 r->out.query->info3 = info3;
214 return WERR_UNKNOWN_LEVEL;
217 if (lp_server_role() == ROLE_DOMAIN_BDC) {
224 /*************************************************************************
225 _netr_NetrEnumerateTrustedDomains
226 *************************************************************************/
228 WERROR _netr_NetrEnumerateTrustedDomains(pipes_struct *p,
229 struct netr_NetrEnumerateTrustedDomains *r)
231 struct netr_Blob trusted_domains_blob;
234 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
236 /* set up the Trusted Domain List response */
238 blob = data_blob_talloc_zero(p->mem_ctx, 2);
239 trusted_domains_blob.data = blob.data;
240 trusted_domains_blob.length = blob.length;
242 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
244 *r->out.trusted_domains_blob = trusted_domains_blob;
249 /******************************************************************
250 gets a machine password entry. checks access rights of the host.
251 ******************************************************************/
253 static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
254 uint16_t sec_chan_type, struct dom_sid *sid)
256 struct samu *sampass = NULL;
262 char addr[INET6_ADDRSTRLEN];
265 * Currently this code is redundent as we already have a filter
266 * by hostname list. What this code really needs to do is to
267 * get a hosts allowed/hosts denied list from the SAM database
268 * on a per user basis, and make the access decision there.
269 * I will leave this code here for now as a reminder to implement
270 * this at a later date. JRA.
273 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
274 client_name(get_client_fd()),
275 client_addr(get_client_fd(),addr,sizeof(addr)))) {
276 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
281 if ( !(sampass = samu_new( NULL )) ) {
282 return NT_STATUS_NO_MEMORY;
285 /* JRA. This is ok as it is only used for generating the challenge. */
287 ret = pdb_getsampwnam(sampass, mach_acct);
291 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
292 TALLOC_FREE(sampass);
293 return NT_STATUS_ACCESS_DENIED;
296 acct_ctrl = pdb_get_acct_ctrl(sampass);
297 if (acct_ctrl & ACB_DISABLED) {
298 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
299 TALLOC_FREE(sampass);
300 return NT_STATUS_ACCOUNT_DISABLED;
303 if (!(acct_ctrl & ACB_SVRTRUST) &&
304 !(acct_ctrl & ACB_WSTRUST) &&
305 !(acct_ctrl & ACB_DOMTRUST))
307 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
308 TALLOC_FREE(sampass);
309 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
312 switch (sec_chan_type) {
314 if (!(acct_ctrl & ACB_SVRTRUST)) {
315 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
316 "but not a server trust account\n", mach_acct));
317 TALLOC_FREE(sampass);
318 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
322 if (!(acct_ctrl & ACB_WSTRUST)) {
323 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
324 "but not a workstation trust account\n", mach_acct));
325 TALLOC_FREE(sampass);
326 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
329 case SEC_CHAN_DOMAIN:
330 if (!(acct_ctrl & ACB_DOMTRUST)) {
331 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
332 "but not a interdomain trust account\n", mach_acct));
333 TALLOC_FREE(sampass);
334 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
341 if ((pass = pdb_get_nt_passwd(sampass)) == NULL) {
342 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
343 TALLOC_FREE(sampass);
344 return NT_STATUS_LOGON_FAILURE;
347 memcpy(md4pw->hash, pass, 16);
348 dump_data(5, md4pw->hash, 16);
350 sid_copy(sid, pdb_get_user_sid(sampass));
352 TALLOC_FREE(sampass);
359 /*************************************************************************
360 _netr_ServerReqChallenge
361 *************************************************************************/
363 NTSTATUS _netr_ServerReqChallenge(pipes_struct *p,
364 struct netr_ServerReqChallenge *r)
366 struct netlogon_server_pipe_state *pipe_state =
367 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
370 DEBUG(10,("_netr_ServerReqChallenge: new challenge requested. Clearing old state.\n"));
371 talloc_free(pipe_state);
372 p->private_data = NULL;
375 pipe_state = talloc(p, struct netlogon_server_pipe_state);
376 NT_STATUS_HAVE_NO_MEMORY(pipe_state);
378 pipe_state->client_challenge = *r->in.credentials;
380 generate_random_buffer(pipe_state->server_challenge.data,
381 sizeof(pipe_state->server_challenge.data));
383 *r->out.return_credentials = pipe_state->server_challenge;
385 p->private_data = pipe_state;
390 /*************************************************************************
391 _netr_ServerAuthenticate
392 Create the initial credentials.
393 *************************************************************************/
395 NTSTATUS _netr_ServerAuthenticate(pipes_struct *p,
396 struct netr_ServerAuthenticate *r)
398 struct netr_ServerAuthenticate3 a;
399 uint32_t negotiate_flags = 0;
402 a.in.server_name = r->in.server_name;
403 a.in.account_name = r->in.account_name;
404 a.in.secure_channel_type = r->in.secure_channel_type;
405 a.in.computer_name = r->in.computer_name;
406 a.in.credentials = r->in.credentials;
407 a.in.negotiate_flags = &negotiate_flags;
409 a.out.return_credentials = r->out.return_credentials;
411 a.out.negotiate_flags = &negotiate_flags;
413 return _netr_ServerAuthenticate3(p, &a);
417 /*************************************************************************
418 _netr_ServerAuthenticate3
419 *************************************************************************/
421 NTSTATUS _netr_ServerAuthenticate3(pipes_struct *p,
422 struct netr_ServerAuthenticate3 *r)
426 /* r->in.negotiate_flags is an aliased pointer to r->out.negotiate_flags,
427 * so use a copy to avoid destroying the client values. */
428 uint32_t in_neg_flags = *r->in.negotiate_flags;
431 struct samr_Password mach_pwd;
432 struct netlogon_creds_CredentialState *creds;
433 struct netlogon_server_pipe_state *pipe_state =
434 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
436 /* According to Microsoft (see bugid #6099)
437 * Windows 7 looks at the negotiate_flags
438 * returned in this structure *even if the
439 * call fails with access denied* ! So in order
440 * to allow Win7 to connect to a Samba NT style
441 * PDC we set the flags before we know if it's
446 srv_flgs = NETLOGON_NEG_ACCOUNT_LOCKOUT |
447 NETLOGON_NEG_PERSISTENT_SAMREPL |
448 NETLOGON_NEG_ARCFOUR |
449 NETLOGON_NEG_PROMOTION_COUNT |
450 NETLOGON_NEG_CHANGELOG_BDC |
451 NETLOGON_NEG_FULL_SYNC_REPL |
452 NETLOGON_NEG_MULTIPLE_SIDS |
454 NETLOGON_NEG_PASSWORD_CHANGE_REFUSAL |
455 NETLOGON_NEG_PASSWORD_SET2;
457 /* Ensure we support strong (128-bit) keys. */
458 if (in_neg_flags & NETLOGON_NEG_STRONG_KEYS) {
459 srv_flgs |= NETLOGON_NEG_STRONG_KEYS;
462 if (lp_server_schannel() != false) {
463 srv_flgs |= NETLOGON_NEG_SCHANNEL;
466 switch (p->hdr_req.opnum) {
467 case NDR_NETR_SERVERAUTHENTICATE:
468 fn = "_netr_ServerAuthenticate";
470 case NDR_NETR_SERVERAUTHENTICATE2:
471 fn = "_netr_ServerAuthenticate2";
473 case NDR_NETR_SERVERAUTHENTICATE3:
474 fn = "_netr_ServerAuthenticate3";
477 return NT_STATUS_INTERNAL_ERROR;
480 /* We use this as the key to store the creds: */
481 /* r->in.computer_name */
484 DEBUG(0,("%s: no challenge sent to client %s\n", fn,
485 r->in.computer_name));
486 status = NT_STATUS_ACCESS_DENIED;
490 if ( (lp_server_schannel() == true) &&
491 ((in_neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
493 /* schannel must be used, but client did not offer it. */
494 DEBUG(0,("%s: schannel required but client failed "
495 "to offer it. Client was %s\n",
496 fn, r->in.account_name));
497 status = NT_STATUS_ACCESS_DENIED;
501 status = get_md4pw(&mach_pwd,
503 r->in.secure_channel_type,
505 if (!NT_STATUS_IS_OK(status)) {
506 DEBUG(0,("%s: failed to get machine password for "
508 fn, r->in.account_name, nt_errstr(status) ));
509 /* always return NT_STATUS_ACCESS_DENIED */
510 status = NT_STATUS_ACCESS_DENIED;
514 /* From the client / server challenges and md4 password, generate sess key */
515 /* Check client credentials are valid. */
516 creds = netlogon_creds_server_init(p->mem_ctx,
519 r->in.secure_channel_type,
520 &pipe_state->client_challenge,
521 &pipe_state->server_challenge,
524 r->out.return_credentials,
525 *r->in.negotiate_flags);
527 DEBUG(0,("%s: netlogon_creds_server_check failed. Rejecting auth "
528 "request from client %s machine account %s\n",
529 fn, r->in.computer_name,
530 r->in.account_name));
531 status = NT_STATUS_ACCESS_DENIED;
535 creds->sid = sid_dup_talloc(creds, &sid);
537 status = NT_STATUS_NO_MEMORY;
541 /* Store off the state so we can continue after client disconnect. */
543 status = schannel_store_session_key(p->mem_ctx, creds);
546 if (!NT_STATUS_IS_OK(status)) {
550 sid_peek_rid(&sid, r->out.rid);
552 status = NT_STATUS_OK;
556 *r->out.negotiate_flags = srv_flgs;
560 /*************************************************************************
561 _netr_ServerAuthenticate2
562 *************************************************************************/
564 NTSTATUS _netr_ServerAuthenticate2(pipes_struct *p,
565 struct netr_ServerAuthenticate2 *r)
567 struct netr_ServerAuthenticate3 a;
570 a.in.server_name = r->in.server_name;
571 a.in.account_name = r->in.account_name;
572 a.in.secure_channel_type = r->in.secure_channel_type;
573 a.in.computer_name = r->in.computer_name;
574 a.in.credentials = r->in.credentials;
575 a.in.negotiate_flags = r->in.negotiate_flags;
577 a.out.return_credentials = r->out.return_credentials;
579 a.out.negotiate_flags = r->out.negotiate_flags;
581 return _netr_ServerAuthenticate3(p, &a);
584 /*************************************************************************
585 *************************************************************************/
587 static NTSTATUS netr_creds_server_step_check(pipes_struct *p,
589 const char *computer_name,
590 struct netr_Authenticator *received_authenticator,
591 struct netr_Authenticator *return_authenticator,
592 struct netlogon_creds_CredentialState **creds_out)
595 struct tdb_context *tdb;
596 bool schannel_global_required = (lp_server_schannel() == true) ? true:false;
597 bool schannel_in_use = (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL) ? true:false; /* &&
598 (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY ||
599 p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY); */
601 tdb = open_schannel_session_store(mem_ctx);
603 return NT_STATUS_ACCESS_DENIED;
606 status = schannel_creds_server_step_check_tdb(tdb, mem_ctx,
608 schannel_global_required,
610 received_authenticator,
611 return_authenticator,
618 /*************************************************************************
619 *************************************************************************/
621 static NTSTATUS netr_find_machine_account(TALLOC_CTX *mem_ctx,
622 const char *account_name,
623 struct samu **sampassp)
625 struct samu *sampass;
629 sampass = samu_new(mem_ctx);
631 return NT_STATUS_NO_MEMORY;
635 ret = pdb_getsampwnam(sampass, account_name);
639 TALLOC_FREE(sampass);
640 return NT_STATUS_ACCESS_DENIED;
643 /* Ensure the account exists and is a machine account. */
645 acct_ctrl = pdb_get_acct_ctrl(sampass);
647 if (!(acct_ctrl & ACB_WSTRUST ||
648 acct_ctrl & ACB_SVRTRUST ||
649 acct_ctrl & ACB_DOMTRUST)) {
650 TALLOC_FREE(sampass);
651 return NT_STATUS_NO_SUCH_USER;
654 if (acct_ctrl & ACB_DISABLED) {
655 TALLOC_FREE(sampass);
656 return NT_STATUS_ACCOUNT_DISABLED;
664 /*************************************************************************
665 *************************************************************************/
667 static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
668 struct samu *sampass,
669 DATA_BLOB *plaintext_blob,
670 struct samr_Password *nt_hash,
671 struct samr_Password *lm_hash)
675 const char *plaintext = NULL;
676 size_t plaintext_len;
677 struct samr_Password nt_hash_local;
680 return NT_STATUS_INVALID_PARAMETER;
683 if (plaintext_blob) {
684 if (!convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
685 plaintext_blob->data, plaintext_blob->length,
686 &plaintext, &plaintext_len, false))
689 mdfour(nt_hash_local.hash, plaintext_blob->data, plaintext_blob->length);
690 nt_hash = &nt_hash_local;
695 if (!pdb_set_plaintext_passwd(sampass, plaintext)) {
696 return NT_STATUS_ACCESS_DENIED;
703 old_pw = pdb_get_nt_passwd(sampass);
705 if (old_pw && memcmp(nt_hash->hash, old_pw, 16) == 0) {
706 /* Avoid backend modificiations and other fun if the
707 client changed the password to the *same thing* */
709 /* LM password should be NULL for machines */
710 if (!pdb_set_lanman_passwd(sampass, NULL, PDB_CHANGED)) {
711 return NT_STATUS_NO_MEMORY;
713 if (!pdb_set_nt_passwd(sampass, nt_hash->hash, PDB_CHANGED)) {
714 return NT_STATUS_NO_MEMORY;
717 if (!pdb_set_pass_last_set_time(sampass, time(NULL), PDB_CHANGED)) {
718 /* Not quite sure what this one qualifies as, but this will do */
719 return NT_STATUS_UNSUCCESSFUL;
726 status = pdb_update_sam_account(sampass);
732 /*************************************************************************
733 _netr_ServerPasswordSet
734 *************************************************************************/
736 NTSTATUS _netr_ServerPasswordSet(pipes_struct *p,
737 struct netr_ServerPasswordSet *r)
739 NTSTATUS status = NT_STATUS_OK;
740 struct samu *sampass=NULL;
742 struct netlogon_creds_CredentialState *creds;
744 DEBUG(5,("_netr_ServerPasswordSet: %d\n", __LINE__));
747 status = netr_creds_server_step_check(p, p->mem_ctx,
750 r->out.return_authenticator,
754 if (!NT_STATUS_IS_OK(status)) {
755 DEBUG(2,("_netr_ServerPasswordSet: netlogon_creds_server_step failed. Rejecting auth "
756 "request from client %s machine account %s\n",
757 r->in.computer_name, creds->computer_name));
762 DEBUG(3,("_netr_ServerPasswordSet: Server Password Set by remote machine:[%s] on account [%s]\n",
763 r->in.computer_name, creds->computer_name));
765 netlogon_creds_des_decrypt(creds, r->in.new_password);
767 DEBUG(100,("_netr_ServerPasswordSet: new given value was :\n"));
768 for(i = 0; i < sizeof(r->in.new_password->hash); i++)
769 DEBUG(100,("%02X ", r->in.new_password->hash[i]));
772 status = netr_find_machine_account(p->mem_ctx,
775 if (!NT_STATUS_IS_OK(status)) {
779 status = netr_set_machine_account_password(sampass,
784 TALLOC_FREE(sampass);
788 /****************************************************************
789 _netr_ServerPasswordSet2
790 ****************************************************************/
792 NTSTATUS _netr_ServerPasswordSet2(pipes_struct *p,
793 struct netr_ServerPasswordSet2 *r)
796 struct netlogon_creds_CredentialState *creds;
797 struct samu *sampass;
799 struct samr_CryptPassword password_buf;
802 status = netr_creds_server_step_check(p, p->mem_ctx,
805 r->out.return_authenticator,
809 if (!NT_STATUS_IS_OK(status)) {
810 DEBUG(2,("_netr_ServerPasswordSet2: netlogon_creds_server_step "
811 "failed. Rejecting auth request from client %s machine account %s\n",
812 r->in.computer_name, creds->computer_name));
817 memcpy(password_buf.data, r->in.new_password->data, 512);
818 SIVAL(password_buf.data, 512, r->in.new_password->length);
819 netlogon_creds_arcfour_crypt(creds, password_buf.data, 516);
821 if (!extract_pw_from_buffer(p->mem_ctx, password_buf.data, &plaintext)) {
822 return NT_STATUS_WRONG_PASSWORD;
825 status = netr_find_machine_account(p->mem_ctx,
828 if (!NT_STATUS_IS_OK(status)) {
832 status = netr_set_machine_account_password(sampass,
837 TALLOC_FREE(sampass);
841 /*************************************************************************
843 *************************************************************************/
845 NTSTATUS _netr_LogonSamLogoff(pipes_struct *p,
846 struct netr_LogonSamLogoff *r)
849 struct netlogon_creds_CredentialState *creds;
852 status = netr_creds_server_step_check(p, p->mem_ctx,
855 r->out.return_authenticator,
862 /*************************************************************************
863 _netr_LogonSamLogon_base
864 *************************************************************************/
866 static NTSTATUS _netr_LogonSamLogon_base(pipes_struct *p,
867 struct netr_LogonSamLogonEx *r,
868 struct netlogon_creds_CredentialState *creds)
870 NTSTATUS status = NT_STATUS_OK;
871 union netr_LogonLevel *logon = r->in.logon;
872 const char *nt_username, *nt_domain, *nt_workstation;
873 auth_usersupplied_info *user_info = NULL;
874 auth_serversupplied_info *server_info = NULL;
875 struct auth_context *auth_context = NULL;
876 uint8_t pipe_session_key[16];
877 bool process_creds = true;
880 switch (p->hdr_req.opnum) {
881 case NDR_NETR_LOGONSAMLOGON:
882 process_creds = true;
883 fn = "_netr_LogonSamLogon";
885 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
886 process_creds = true;
887 fn = "_netr_LogonSamLogonWithFlags";
889 case NDR_NETR_LOGONSAMLOGONEX:
890 process_creds = false;
891 fn = "_netr_LogonSamLogonEx";
894 return NT_STATUS_INTERNAL_ERROR;
897 *r->out.authoritative = true; /* authoritative response */
899 switch (r->in.validation_level) {
901 r->out.validation->sam2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo2);
902 if (!r->out.validation->sam2) {
903 return NT_STATUS_NO_MEMORY;
907 r->out.validation->sam3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo3);
908 if (!r->out.validation->sam3) {
909 return NT_STATUS_NO_MEMORY;
913 DEBUG(0,("%s: bad validation_level value %d.\n",
914 fn, (int)r->in.validation_level));
915 return NT_STATUS_INVALID_INFO_CLASS;
918 switch (r->in.logon_level) {
919 case NetlogonInteractiveInformation:
920 nt_username = logon->password->identity_info.account_name.string;
921 nt_domain = logon->password->identity_info.domain_name.string;
922 nt_workstation = logon->password->identity_info.workstation.string;
924 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
926 case NetlogonNetworkInformation:
927 nt_username = logon->network->identity_info.account_name.string;
928 nt_domain = logon->network->identity_info.domain_name.string;
929 nt_workstation = logon->network->identity_info.workstation.string;
931 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
934 DEBUG(2,("SAM Logon: unsupported switch value\n"));
935 return NT_STATUS_INVALID_INFO_CLASS;
938 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
939 fstrcpy(current_user_info.smb_name, nt_username);
940 sub_set_smb_name(nt_username);
942 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
943 r->in.validation_level, nt_username));
945 status = NT_STATUS_OK;
947 switch (r->in.logon_level) {
948 case NetlogonNetworkInformation:
950 const char *wksname = nt_workstation;
952 status = make_auth_context_fixed(&auth_context,
953 logon->network->challenge);
954 if (!NT_STATUS_IS_OK(status)) {
958 /* For a network logon, the workstation name comes in with two
959 * backslashes in the front. Strip them if they are there. */
961 if (*wksname == '\\') wksname++;
962 if (*wksname == '\\') wksname++;
964 /* Standard challenge/response authenticaion */
965 if (!make_user_info_netlogon_network(&user_info,
966 nt_username, nt_domain,
968 logon->network->identity_info.parameter_control,
969 logon->network->lm.data,
970 logon->network->lm.length,
971 logon->network->nt.data,
972 logon->network->nt.length)) {
973 status = NT_STATUS_NO_MEMORY;
977 case NetlogonInteractiveInformation:
978 /* 'Interactive' authentication, supplies the password in its
979 MD4 form, encrypted with the session key. We will convert
980 this to challenge/response for the auth subsystem to chew
985 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
989 auth_context->get_ntlm_challenge(auth_context, chal);
991 if (!make_user_info_netlogon_interactive(&user_info,
992 nt_username, nt_domain,
994 logon->password->identity_info.parameter_control,
996 logon->password->lmpassword.hash,
997 logon->password->ntpassword.hash,
998 creds->session_key)) {
999 status = NT_STATUS_NO_MEMORY;
1004 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1005 return NT_STATUS_INVALID_INFO_CLASS;
1008 if ( NT_STATUS_IS_OK(status) ) {
1009 status = auth_context->check_ntlm_password(auth_context,
1010 user_info, &server_info);
1013 (auth_context->free)(&auth_context);
1014 free_user_info(&user_info);
1016 DEBUG(5,("%s: check_password returned status %s\n",
1017 fn, nt_errstr(status)));
1019 /* Check account and password */
1021 if (!NT_STATUS_IS_OK(status)) {
1022 /* If we don't know what this domain is, we need to
1023 indicate that we are not authoritative. This
1024 allows the client to decide if it needs to try
1025 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
1026 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1027 && !strequal(nt_domain, get_global_sam_name())
1028 && !is_trusted_domain(nt_domain) )
1029 *r->out.authoritative = false; /* We are not authoritative */
1031 TALLOC_FREE(server_info);
1035 if (server_info->guest) {
1036 /* We don't like guest domain logons... */
1037 DEBUG(5,("%s: Attempted domain logon as GUEST "
1039 TALLOC_FREE(server_info);
1040 return NT_STATUS_LOGON_FAILURE;
1043 /* This is the point at which, if the login was successful, that
1044 the SAM Local Security Authority should record that the user is
1045 logged in to the domain. */
1047 if (process_creds) {
1048 /* Get the pipe session key from the creds. */
1049 memcpy(pipe_session_key, creds->session_key, 16);
1051 /* Get the pipe session key from the schannel. */
1052 if ((p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL)
1053 || (p->auth.a_u.schannel_auth == NULL)) {
1054 return NT_STATUS_INVALID_HANDLE;
1056 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->sess_key, 16);
1059 switch (r->in.validation_level) {
1061 status = serverinfo_to_SamInfo2(server_info, pipe_session_key, 16,
1062 r->out.validation->sam2);
1065 status = serverinfo_to_SamInfo3(server_info, pipe_session_key, 16,
1066 r->out.validation->sam3);
1070 TALLOC_FREE(server_info);
1075 /****************************************************************
1076 _netr_LogonSamLogonWithFlags
1077 ****************************************************************/
1079 NTSTATUS _netr_LogonSamLogonWithFlags(pipes_struct *p,
1080 struct netr_LogonSamLogonWithFlags *r)
1083 struct netlogon_creds_CredentialState *creds;
1084 struct netr_LogonSamLogonEx r2;
1085 struct netr_Authenticator return_authenticator;
1088 status = netr_creds_server_step_check(p, p->mem_ctx,
1089 r->in.computer_name,
1091 &return_authenticator,
1094 if (!NT_STATUS_IS_OK(status)) {
1098 r2.in.server_name = r->in.server_name;
1099 r2.in.computer_name = r->in.computer_name;
1100 r2.in.logon_level = r->in.logon_level;
1101 r2.in.logon = r->in.logon;
1102 r2.in.validation_level = r->in.validation_level;
1103 r2.in.flags = r->in.flags;
1104 r2.out.validation = r->out.validation;
1105 r2.out.authoritative = r->out.authoritative;
1106 r2.out.flags = r->out.flags;
1108 status = _netr_LogonSamLogon_base(p, &r2, creds);
1110 *r->out.return_authenticator = return_authenticator;
1115 /*************************************************************************
1117 *************************************************************************/
1119 NTSTATUS _netr_LogonSamLogon(pipes_struct *p,
1120 struct netr_LogonSamLogon *r)
1123 struct netr_LogonSamLogonWithFlags r2;
1126 r2.in.server_name = r->in.server_name;
1127 r2.in.computer_name = r->in.computer_name;
1128 r2.in.credential = r->in.credential;
1129 r2.in.logon_level = r->in.logon_level;
1130 r2.in.logon = r->in.logon;
1131 r2.in.validation_level = r->in.validation_level;
1132 r2.in.return_authenticator = r->in.return_authenticator;
1133 r2.in.flags = &flags;
1134 r2.out.validation = r->out.validation;
1135 r2.out.authoritative = r->out.authoritative;
1136 r2.out.flags = &flags;
1137 r2.out.return_authenticator = r->out.return_authenticator;
1139 status = _netr_LogonSamLogonWithFlags(p, &r2);
1144 /*************************************************************************
1145 _netr_LogonSamLogonEx
1146 - no credential chaining. Map into net sam logon.
1147 *************************************************************************/
1149 NTSTATUS _netr_LogonSamLogonEx(pipes_struct *p,
1150 struct netr_LogonSamLogonEx *r)
1153 struct netlogon_creds_CredentialState *creds = NULL;
1156 status = schannel_fetch_session_key(p->mem_ctx, r->in.computer_name, &creds);
1158 if (!NT_STATUS_IS_OK(status)) {
1162 /* Only allow this if the pipe is protected. */
1163 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1164 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1165 get_remote_machine_name() ));
1166 return NT_STATUS_INVALID_PARAMETER;
1169 status = _netr_LogonSamLogon_base(p, r, creds);
1175 /*************************************************************************
1177 *************************************************************************/
1178 #if 0 /* JERRY -- not correct */
1179 NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1180 DS_R_ENUM_DOM_TRUSTS *r_u)
1182 NTSTATUS status = NT_STATUS_OK;
1184 /* TODO: According to MSDN, the can only be executed against a
1185 DC or domain member running Windows 2000 or later. Need
1186 to test against a standalone 2k server and see what it
1187 does. A windows 2000 DC includes its own domain in the
1195 /****************************************************************
1196 ****************************************************************/
1198 WERROR _netr_LogonUasLogon(pipes_struct *p,
1199 struct netr_LogonUasLogon *r)
1201 p->rng_fault_state = true;
1202 return WERR_NOT_SUPPORTED;
1205 /****************************************************************
1206 ****************************************************************/
1208 WERROR _netr_LogonUasLogoff(pipes_struct *p,
1209 struct netr_LogonUasLogoff *r)
1211 p->rng_fault_state = true;
1212 return WERR_NOT_SUPPORTED;
1215 /****************************************************************
1216 ****************************************************************/
1218 NTSTATUS _netr_DatabaseDeltas(pipes_struct *p,
1219 struct netr_DatabaseDeltas *r)
1221 p->rng_fault_state = true;
1222 return NT_STATUS_NOT_IMPLEMENTED;
1225 /****************************************************************
1226 ****************************************************************/
1228 NTSTATUS _netr_DatabaseSync(pipes_struct *p,
1229 struct netr_DatabaseSync *r)
1231 p->rng_fault_state = true;
1232 return NT_STATUS_NOT_IMPLEMENTED;
1235 /****************************************************************
1236 ****************************************************************/
1238 NTSTATUS _netr_AccountDeltas(pipes_struct *p,
1239 struct netr_AccountDeltas *r)
1241 p->rng_fault_state = true;
1242 return NT_STATUS_NOT_IMPLEMENTED;
1245 /****************************************************************
1246 ****************************************************************/
1248 NTSTATUS _netr_AccountSync(pipes_struct *p,
1249 struct netr_AccountSync *r)
1251 p->rng_fault_state = true;
1252 return NT_STATUS_NOT_IMPLEMENTED;
1255 /****************************************************************
1256 ****************************************************************/
1258 WERROR _netr_GetDcName(pipes_struct *p,
1259 struct netr_GetDcName *r)
1261 p->rng_fault_state = true;
1262 return WERR_NOT_SUPPORTED;
1265 /****************************************************************
1266 ****************************************************************/
1268 WERROR _netr_GetAnyDCName(pipes_struct *p,
1269 struct netr_GetAnyDCName *r)
1271 p->rng_fault_state = true;
1272 return WERR_NOT_SUPPORTED;
1275 /****************************************************************
1276 ****************************************************************/
1278 NTSTATUS _netr_DatabaseSync2(pipes_struct *p,
1279 struct netr_DatabaseSync2 *r)
1281 p->rng_fault_state = true;
1282 return NT_STATUS_NOT_IMPLEMENTED;
1285 /****************************************************************
1286 ****************************************************************/
1288 NTSTATUS _netr_DatabaseRedo(pipes_struct *p,
1289 struct netr_DatabaseRedo *r)
1291 p->rng_fault_state = true;
1292 return NT_STATUS_NOT_IMPLEMENTED;
1295 /****************************************************************
1296 ****************************************************************/
1298 WERROR _netr_DsRGetDCName(pipes_struct *p,
1299 struct netr_DsRGetDCName *r)
1301 p->rng_fault_state = true;
1302 return WERR_NOT_SUPPORTED;
1305 /****************************************************************
1306 ****************************************************************/
1308 NTSTATUS _netr_LogonGetCapabilities(pipes_struct *p,
1309 struct netr_LogonGetCapabilities *r)
1311 return NT_STATUS_NOT_IMPLEMENTED;
1314 /****************************************************************
1315 ****************************************************************/
1317 WERROR _netr_NETRLOGONSETSERVICEBITS(pipes_struct *p,
1318 struct netr_NETRLOGONSETSERVICEBITS *r)
1320 p->rng_fault_state = true;
1321 return WERR_NOT_SUPPORTED;
1324 /****************************************************************
1325 ****************************************************************/
1327 WERROR _netr_LogonGetTrustRid(pipes_struct *p,
1328 struct netr_LogonGetTrustRid *r)
1330 p->rng_fault_state = true;
1331 return WERR_NOT_SUPPORTED;
1334 /****************************************************************
1335 ****************************************************************/
1337 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(pipes_struct *p,
1338 struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
1340 p->rng_fault_state = true;
1341 return WERR_NOT_SUPPORTED;
1344 /****************************************************************
1345 ****************************************************************/
1347 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(pipes_struct *p,
1348 struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
1350 p->rng_fault_state = true;
1351 return WERR_NOT_SUPPORTED;
1354 /****************************************************************
1355 ****************************************************************/
1357 WERROR _netr_DsRGetDCNameEx(pipes_struct *p,
1358 struct netr_DsRGetDCNameEx *r)
1360 p->rng_fault_state = true;
1361 return WERR_NOT_SUPPORTED;
1364 /****************************************************************
1365 ****************************************************************/
1367 WERROR _netr_DsRGetSiteName(pipes_struct *p,
1368 struct netr_DsRGetSiteName *r)
1370 p->rng_fault_state = true;
1371 return WERR_NOT_SUPPORTED;
1374 /****************************************************************
1375 ****************************************************************/
1377 NTSTATUS _netr_LogonGetDomainInfo(pipes_struct *p,
1378 struct netr_LogonGetDomainInfo *r)
1380 p->rng_fault_state = true;
1381 return NT_STATUS_NOT_IMPLEMENTED;
1384 /****************************************************************
1385 ****************************************************************/
1387 WERROR _netr_ServerPasswordGet(pipes_struct *p,
1388 struct netr_ServerPasswordGet *r)
1390 p->rng_fault_state = true;
1391 return WERR_NOT_SUPPORTED;
1394 /****************************************************************
1395 ****************************************************************/
1397 WERROR _netr_NETRLOGONSENDTOSAM(pipes_struct *p,
1398 struct netr_NETRLOGONSENDTOSAM *r)
1400 p->rng_fault_state = true;
1401 return WERR_NOT_SUPPORTED;
1404 /****************************************************************
1405 ****************************************************************/
1407 WERROR _netr_DsRAddressToSitenamesW(pipes_struct *p,
1408 struct netr_DsRAddressToSitenamesW *r)
1410 p->rng_fault_state = true;
1411 return WERR_NOT_SUPPORTED;
1414 /****************************************************************
1415 ****************************************************************/
1417 WERROR _netr_DsRGetDCNameEx2(pipes_struct *p,
1418 struct netr_DsRGetDCNameEx2 *r)
1420 p->rng_fault_state = true;
1421 return WERR_NOT_SUPPORTED;
1424 /****************************************************************
1425 ****************************************************************/
1427 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(pipes_struct *p,
1428 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
1430 p->rng_fault_state = true;
1431 return WERR_NOT_SUPPORTED;
1434 /****************************************************************
1435 ****************************************************************/
1437 WERROR _netr_NetrEnumerateTrustedDomainsEx(pipes_struct *p,
1438 struct netr_NetrEnumerateTrustedDomainsEx *r)
1440 p->rng_fault_state = true;
1441 return WERR_NOT_SUPPORTED;
1444 /****************************************************************
1445 ****************************************************************/
1447 WERROR _netr_DsRAddressToSitenamesExW(pipes_struct *p,
1448 struct netr_DsRAddressToSitenamesExW *r)
1450 p->rng_fault_state = true;
1451 return WERR_NOT_SUPPORTED;
1454 /****************************************************************
1455 ****************************************************************/
1457 WERROR _netr_DsrGetDcSiteCoverageW(pipes_struct *p,
1458 struct netr_DsrGetDcSiteCoverageW *r)
1460 p->rng_fault_state = true;
1461 return WERR_NOT_SUPPORTED;
1464 /****************************************************************
1465 ****************************************************************/
1467 WERROR _netr_DsrEnumerateDomainTrusts(pipes_struct *p,
1468 struct netr_DsrEnumerateDomainTrusts *r)
1470 p->rng_fault_state = true;
1471 return WERR_NOT_SUPPORTED;
1474 /****************************************************************
1475 ****************************************************************/
1477 WERROR _netr_DsrDeregisterDNSHostRecords(pipes_struct *p,
1478 struct netr_DsrDeregisterDNSHostRecords *r)
1480 p->rng_fault_state = true;
1481 return WERR_NOT_SUPPORTED;
1484 /****************************************************************
1485 ****************************************************************/
1487 NTSTATUS _netr_ServerTrustPasswordsGet(pipes_struct *p,
1488 struct netr_ServerTrustPasswordsGet *r)
1490 p->rng_fault_state = true;
1491 return NT_STATUS_NOT_IMPLEMENTED;
1494 /****************************************************************
1495 ****************************************************************/
1497 WERROR _netr_DsRGetForestTrustInformation(pipes_struct *p,
1498 struct netr_DsRGetForestTrustInformation *r)
1500 p->rng_fault_state = true;
1501 return WERR_NOT_SUPPORTED;
1504 /****************************************************************
1505 ****************************************************************/
1507 WERROR _netr_GetForestTrustInformation(pipes_struct *p,
1508 struct netr_GetForestTrustInformation *r)
1510 p->rng_fault_state = true;
1511 return WERR_NOT_SUPPORTED;
1514 /****************************************************************
1515 ****************************************************************/
1517 NTSTATUS _netr_ServerGetTrustInfo(pipes_struct *p,
1518 struct netr_ServerGetTrustInfo *r)
1520 p->rng_fault_state = true;
1521 return NT_STATUS_NOT_IMPLEMENTED;