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 ) )
141 if ( !get_dc_name( domain, NULL, dc_name2, &dc_ss ) ) {
142 tc_status = WERR_NO_LOGON_SERVERS;
146 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
155 case NETLOGON_CONTROL_REDISCOVER:
156 domain = r->in.data->domain;
158 if ( !is_trusted_domain( domain ) )
161 if ( !get_dc_name( domain, NULL, dc_name2, &dc_ss ) ) {
162 tc_status = WERR_NO_LOGON_SERVERS;
166 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
176 /* no idea what this should be */
177 DEBUG(0,("%s: unimplemented function level [%d]\n",
178 fn, r->in.function_code));
179 return WERR_UNKNOWN_LEVEL;
182 /* prepare the response */
184 switch (r->in.level) {
186 info1 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_1);
187 W_ERROR_HAVE_NO_MEMORY(info1);
189 info1->flags = flags;
190 info1->pdc_connection_status = pdc_connection_status;
192 r->out.query->info1 = info1;
195 info2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_2);
196 W_ERROR_HAVE_NO_MEMORY(info2);
198 info2->flags = flags;
199 info2->pdc_connection_status = pdc_connection_status;
200 info2->trusted_dc_name = dc_name;
201 info2->tc_connection_status = tc_status;
203 r->out.query->info2 = info2;
206 info3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_3);
207 W_ERROR_HAVE_NO_MEMORY(info3);
209 info3->flags = flags;
210 info3->logon_attempts = logon_attempts;
212 r->out.query->info3 = info3;
215 return WERR_UNKNOWN_LEVEL;
218 if (lp_server_role() == ROLE_DOMAIN_BDC) {
225 /*************************************************************************
226 _netr_NetrEnumerateTrustedDomains
227 *************************************************************************/
229 WERROR _netr_NetrEnumerateTrustedDomains(pipes_struct *p,
230 struct netr_NetrEnumerateTrustedDomains *r)
232 struct netr_Blob trusted_domains_blob;
235 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
237 /* set up the Trusted Domain List response */
239 blob = data_blob_talloc_zero(p->mem_ctx, 2);
240 trusted_domains_blob.data = blob.data;
241 trusted_domains_blob.length = blob.length;
243 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
245 *r->out.trusted_domains_blob = trusted_domains_blob;
250 /******************************************************************
251 gets a machine password entry. checks access rights of the host.
252 ******************************************************************/
254 static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
255 uint16_t sec_chan_type, struct dom_sid *sid)
257 struct samu *sampass = NULL;
263 char addr[INET6_ADDRSTRLEN];
266 * Currently this code is redundent as we already have a filter
267 * by hostname list. What this code really needs to do is to
268 * get a hosts allowed/hosts denied list from the SAM database
269 * on a per user basis, and make the access decision there.
270 * I will leave this code here for now as a reminder to implement
271 * this at a later date. JRA.
274 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
275 client_name(get_client_fd()),
276 client_addr(get_client_fd(),addr,sizeof(addr)))) {
277 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
282 if ( !(sampass = samu_new( NULL )) ) {
283 return NT_STATUS_NO_MEMORY;
286 /* JRA. This is ok as it is only used for generating the challenge. */
288 ret = pdb_getsampwnam(sampass, mach_acct);
292 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
293 TALLOC_FREE(sampass);
294 return NT_STATUS_ACCESS_DENIED;
297 acct_ctrl = pdb_get_acct_ctrl(sampass);
298 if (acct_ctrl & ACB_DISABLED) {
299 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
300 TALLOC_FREE(sampass);
301 return NT_STATUS_ACCOUNT_DISABLED;
304 if (!(acct_ctrl & ACB_SVRTRUST) &&
305 !(acct_ctrl & ACB_WSTRUST) &&
306 !(acct_ctrl & ACB_DOMTRUST))
308 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
309 TALLOC_FREE(sampass);
310 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
313 switch (sec_chan_type) {
315 if (!(acct_ctrl & ACB_SVRTRUST)) {
316 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
317 "but not a server trust account\n", mach_acct));
318 TALLOC_FREE(sampass);
319 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
323 if (!(acct_ctrl & ACB_WSTRUST)) {
324 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
325 "but not a workstation trust account\n", mach_acct));
326 TALLOC_FREE(sampass);
327 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
330 case SEC_CHAN_DOMAIN:
331 if (!(acct_ctrl & ACB_DOMTRUST)) {
332 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
333 "but not a interdomain trust account\n", mach_acct));
334 TALLOC_FREE(sampass);
335 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
342 if ((pass = pdb_get_nt_passwd(sampass)) == NULL) {
343 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
344 TALLOC_FREE(sampass);
345 return NT_STATUS_LOGON_FAILURE;
348 memcpy(md4pw->hash, pass, 16);
349 dump_data(5, md4pw->hash, 16);
351 sid_copy(sid, pdb_get_user_sid(sampass));
353 TALLOC_FREE(sampass);
360 /*************************************************************************
361 _netr_ServerReqChallenge
362 *************************************************************************/
364 NTSTATUS _netr_ServerReqChallenge(pipes_struct *p,
365 struct netr_ServerReqChallenge *r)
367 struct netlogon_server_pipe_state *pipe_state =
368 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
371 DEBUG(10,("_netr_ServerReqChallenge: new challenge requested. Clearing old state.\n"));
372 talloc_free(pipe_state);
373 p->private_data = NULL;
376 pipe_state = talloc(p, struct netlogon_server_pipe_state);
377 NT_STATUS_HAVE_NO_MEMORY(pipe_state);
379 pipe_state->client_challenge = *r->in.credentials;
381 generate_random_buffer(pipe_state->server_challenge.data,
382 sizeof(pipe_state->server_challenge.data));
384 *r->out.return_credentials = pipe_state->server_challenge;
386 p->private_data = pipe_state;
391 /*************************************************************************
392 _netr_ServerAuthenticate
393 Create the initial credentials.
394 *************************************************************************/
396 NTSTATUS _netr_ServerAuthenticate(pipes_struct *p,
397 struct netr_ServerAuthenticate *r)
399 struct netr_ServerAuthenticate3 a;
400 uint32_t negotiate_flags = 0;
403 a.in.server_name = r->in.server_name;
404 a.in.account_name = r->in.account_name;
405 a.in.secure_channel_type = r->in.secure_channel_type;
406 a.in.computer_name = r->in.computer_name;
407 a.in.credentials = r->in.credentials;
408 a.in.negotiate_flags = &negotiate_flags;
410 a.out.return_credentials = r->out.return_credentials;
412 a.out.negotiate_flags = &negotiate_flags;
414 return _netr_ServerAuthenticate3(p, &a);
418 /*************************************************************************
419 _netr_ServerAuthenticate3
420 *************************************************************************/
422 NTSTATUS _netr_ServerAuthenticate3(pipes_struct *p,
423 struct netr_ServerAuthenticate3 *r)
427 /* r->in.negotiate_flags is an aliased pointer to r->out.negotiate_flags,
428 * so use a copy to avoid destroying the client values. */
429 uint32_t in_neg_flags = *r->in.negotiate_flags;
432 struct samr_Password mach_pwd;
433 struct netlogon_creds_CredentialState *creds;
434 struct netlogon_server_pipe_state *pipe_state =
435 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
437 /* According to Microsoft (see bugid #6099)
438 * Windows 7 looks at the negotiate_flags
439 * returned in this structure *even if the
440 * call fails with access denied* ! So in order
441 * to allow Win7 to connect to a Samba NT style
442 * PDC we set the flags before we know if it's
447 srv_flgs = NETLOGON_NEG_ACCOUNT_LOCKOUT |
448 NETLOGON_NEG_PERSISTENT_SAMREPL |
449 NETLOGON_NEG_ARCFOUR |
450 NETLOGON_NEG_PROMOTION_COUNT |
451 NETLOGON_NEG_CHANGELOG_BDC |
452 NETLOGON_NEG_FULL_SYNC_REPL |
453 NETLOGON_NEG_MULTIPLE_SIDS |
455 NETLOGON_NEG_PASSWORD_CHANGE_REFUSAL |
456 NETLOGON_NEG_PASSWORD_SET2;
458 /* Ensure we support strong (128-bit) keys. */
459 if (in_neg_flags & NETLOGON_NEG_STRONG_KEYS) {
460 srv_flgs |= NETLOGON_NEG_STRONG_KEYS;
463 if (lp_server_schannel() != false) {
464 srv_flgs |= NETLOGON_NEG_SCHANNEL;
467 switch (p->hdr_req.opnum) {
468 case NDR_NETR_SERVERAUTHENTICATE:
469 fn = "_netr_ServerAuthenticate";
471 case NDR_NETR_SERVERAUTHENTICATE2:
472 fn = "_netr_ServerAuthenticate2";
474 case NDR_NETR_SERVERAUTHENTICATE3:
475 fn = "_netr_ServerAuthenticate3";
478 return NT_STATUS_INTERNAL_ERROR;
481 /* We use this as the key to store the creds: */
482 /* r->in.computer_name */
485 DEBUG(0,("%s: no challenge sent to client %s\n", fn,
486 r->in.computer_name));
487 status = NT_STATUS_ACCESS_DENIED;
491 if ( (lp_server_schannel() == true) &&
492 ((in_neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
494 /* schannel must be used, but client did not offer it. */
495 DEBUG(0,("%s: schannel required but client failed "
496 "to offer it. Client was %s\n",
497 fn, r->in.account_name));
498 status = NT_STATUS_ACCESS_DENIED;
502 status = get_md4pw(&mach_pwd,
504 r->in.secure_channel_type,
506 if (!NT_STATUS_IS_OK(status)) {
507 DEBUG(0,("%s: failed to get machine password for "
509 fn, r->in.account_name, nt_errstr(status) ));
510 /* always return NT_STATUS_ACCESS_DENIED */
511 status = NT_STATUS_ACCESS_DENIED;
515 /* From the client / server challenges and md4 password, generate sess key */
516 /* Check client credentials are valid. */
517 creds = netlogon_creds_server_init(p->mem_ctx,
520 r->in.secure_channel_type,
521 &pipe_state->client_challenge,
522 &pipe_state->server_challenge,
525 r->out.return_credentials,
526 *r->in.negotiate_flags);
528 DEBUG(0,("%s: netlogon_creds_server_check failed. Rejecting auth "
529 "request from client %s machine account %s\n",
530 fn, r->in.computer_name,
531 r->in.account_name));
532 status = NT_STATUS_ACCESS_DENIED;
536 creds->sid = sid_dup_talloc(creds, &sid);
538 status = NT_STATUS_NO_MEMORY;
542 /* Store off the state so we can continue after client disconnect. */
544 status = schannel_store_session_key(p->mem_ctx, creds);
547 if (!NT_STATUS_IS_OK(status)) {
551 sid_peek_rid(&sid, r->out.rid);
553 status = NT_STATUS_OK;
557 *r->out.negotiate_flags = srv_flgs;
561 /*************************************************************************
562 _netr_ServerAuthenticate2
563 *************************************************************************/
565 NTSTATUS _netr_ServerAuthenticate2(pipes_struct *p,
566 struct netr_ServerAuthenticate2 *r)
568 struct netr_ServerAuthenticate3 a;
571 a.in.server_name = r->in.server_name;
572 a.in.account_name = r->in.account_name;
573 a.in.secure_channel_type = r->in.secure_channel_type;
574 a.in.computer_name = r->in.computer_name;
575 a.in.credentials = r->in.credentials;
576 a.in.negotiate_flags = r->in.negotiate_flags;
578 a.out.return_credentials = r->out.return_credentials;
580 a.out.negotiate_flags = r->out.negotiate_flags;
582 return _netr_ServerAuthenticate3(p, &a);
585 /*************************************************************************
586 *************************************************************************/
588 static NTSTATUS netr_creds_server_step_check(pipes_struct *p,
590 const char *computer_name,
591 struct netr_Authenticator *received_authenticator,
592 struct netr_Authenticator *return_authenticator,
593 struct netlogon_creds_CredentialState **creds_out)
596 struct tdb_context *tdb;
597 bool schannel_global_required = (lp_server_schannel() == true) ? true:false;
598 bool schannel_in_use = (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL) ? true:false; /* &&
599 (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY ||
600 p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY); */
602 tdb = open_schannel_session_store(mem_ctx);
604 return NT_STATUS_ACCESS_DENIED;
607 status = schannel_creds_server_step_check_tdb(tdb, mem_ctx,
609 schannel_global_required,
611 received_authenticator,
612 return_authenticator,
619 /*************************************************************************
620 *************************************************************************/
622 static NTSTATUS netr_find_machine_account(TALLOC_CTX *mem_ctx,
623 const char *account_name,
624 struct samu **sampassp)
626 struct samu *sampass;
630 sampass = samu_new(mem_ctx);
632 return NT_STATUS_NO_MEMORY;
636 ret = pdb_getsampwnam(sampass, account_name);
640 TALLOC_FREE(sampass);
641 return NT_STATUS_ACCESS_DENIED;
644 /* Ensure the account exists and is a machine account. */
646 acct_ctrl = pdb_get_acct_ctrl(sampass);
648 if (!(acct_ctrl & ACB_WSTRUST ||
649 acct_ctrl & ACB_SVRTRUST ||
650 acct_ctrl & ACB_DOMTRUST)) {
651 TALLOC_FREE(sampass);
652 return NT_STATUS_NO_SUCH_USER;
655 if (acct_ctrl & ACB_DISABLED) {
656 TALLOC_FREE(sampass);
657 return NT_STATUS_ACCOUNT_DISABLED;
665 /*************************************************************************
666 *************************************************************************/
668 static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
669 struct samu *sampass,
670 DATA_BLOB *plaintext_blob,
671 struct samr_Password *nt_hash,
672 struct samr_Password *lm_hash)
676 const char *plaintext = NULL;
677 size_t plaintext_len;
678 struct samr_Password nt_hash_local;
681 return NT_STATUS_INVALID_PARAMETER;
684 if (plaintext_blob) {
685 if (!convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
686 plaintext_blob->data, plaintext_blob->length,
687 &plaintext, &plaintext_len, false))
690 mdfour(nt_hash_local.hash, plaintext_blob->data, plaintext_blob->length);
691 nt_hash = &nt_hash_local;
696 if (!pdb_set_plaintext_passwd(sampass, plaintext)) {
697 return NT_STATUS_ACCESS_DENIED;
704 old_pw = pdb_get_nt_passwd(sampass);
706 if (old_pw && memcmp(nt_hash->hash, old_pw, 16) == 0) {
707 /* Avoid backend modificiations and other fun if the
708 client changed the password to the *same thing* */
710 /* LM password should be NULL for machines */
711 if (!pdb_set_lanman_passwd(sampass, NULL, PDB_CHANGED)) {
712 return NT_STATUS_NO_MEMORY;
714 if (!pdb_set_nt_passwd(sampass, nt_hash->hash, PDB_CHANGED)) {
715 return NT_STATUS_NO_MEMORY;
718 if (!pdb_set_pass_last_set_time(sampass, time(NULL), PDB_CHANGED)) {
719 /* Not quite sure what this one qualifies as, but this will do */
720 return NT_STATUS_UNSUCCESSFUL;
727 status = pdb_update_sam_account(sampass);
733 /*************************************************************************
734 _netr_ServerPasswordSet
735 *************************************************************************/
737 NTSTATUS _netr_ServerPasswordSet(pipes_struct *p,
738 struct netr_ServerPasswordSet *r)
740 NTSTATUS status = NT_STATUS_OK;
741 struct samu *sampass=NULL;
743 struct netlogon_creds_CredentialState *creds;
745 DEBUG(5,("_netr_ServerPasswordSet: %d\n", __LINE__));
748 status = netr_creds_server_step_check(p, p->mem_ctx,
751 r->out.return_authenticator,
755 if (!NT_STATUS_IS_OK(status)) {
756 DEBUG(2,("_netr_ServerPasswordSet: netlogon_creds_server_step failed. Rejecting auth "
757 "request from client %s machine account %s\n",
758 r->in.computer_name, creds->computer_name));
763 DEBUG(3,("_netr_ServerPasswordSet: Server Password Set by remote machine:[%s] on account [%s]\n",
764 r->in.computer_name, creds->computer_name));
766 netlogon_creds_des_decrypt(creds, r->in.new_password);
768 DEBUG(100,("_netr_ServerPasswordSet: new given value was :\n"));
769 for(i = 0; i < sizeof(r->in.new_password->hash); i++)
770 DEBUG(100,("%02X ", r->in.new_password->hash[i]));
773 status = netr_find_machine_account(p->mem_ctx,
776 if (!NT_STATUS_IS_OK(status)) {
780 status = netr_set_machine_account_password(sampass,
785 TALLOC_FREE(sampass);
789 /****************************************************************
790 _netr_ServerPasswordSet2
791 ****************************************************************/
793 NTSTATUS _netr_ServerPasswordSet2(pipes_struct *p,
794 struct netr_ServerPasswordSet2 *r)
797 struct netlogon_creds_CredentialState *creds;
798 struct samu *sampass;
800 struct samr_CryptPassword password_buf;
803 status = netr_creds_server_step_check(p, p->mem_ctx,
806 r->out.return_authenticator,
810 if (!NT_STATUS_IS_OK(status)) {
811 DEBUG(2,("_netr_ServerPasswordSet2: netlogon_creds_server_step "
812 "failed. Rejecting auth request from client %s machine account %s\n",
813 r->in.computer_name, creds->computer_name));
818 memcpy(password_buf.data, r->in.new_password->data, 512);
819 SIVAL(password_buf.data, 512, r->in.new_password->length);
820 netlogon_creds_arcfour_crypt(creds, password_buf.data, 516);
822 if (!extract_pw_from_buffer(p->mem_ctx, password_buf.data, &plaintext)) {
823 return NT_STATUS_WRONG_PASSWORD;
826 status = netr_find_machine_account(p->mem_ctx,
829 if (!NT_STATUS_IS_OK(status)) {
833 status = netr_set_machine_account_password(sampass,
838 TALLOC_FREE(sampass);
842 /*************************************************************************
844 *************************************************************************/
846 NTSTATUS _netr_LogonSamLogoff(pipes_struct *p,
847 struct netr_LogonSamLogoff *r)
850 struct netlogon_creds_CredentialState *creds;
853 status = netr_creds_server_step_check(p, p->mem_ctx,
856 r->out.return_authenticator,
863 /*************************************************************************
864 _netr_LogonSamLogon_base
865 *************************************************************************/
867 static NTSTATUS _netr_LogonSamLogon_base(pipes_struct *p,
868 struct netr_LogonSamLogonEx *r,
869 struct netlogon_creds_CredentialState *creds)
871 NTSTATUS status = NT_STATUS_OK;
872 union netr_LogonLevel *logon = r->in.logon;
873 const char *nt_username, *nt_domain, *nt_workstation;
874 auth_usersupplied_info *user_info = NULL;
875 auth_serversupplied_info *server_info = NULL;
876 struct auth_context *auth_context = NULL;
877 uint8_t pipe_session_key[16];
878 bool process_creds = true;
881 switch (p->hdr_req.opnum) {
882 case NDR_NETR_LOGONSAMLOGON:
883 process_creds = true;
884 fn = "_netr_LogonSamLogon";
886 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
887 process_creds = true;
888 fn = "_netr_LogonSamLogonWithFlags";
890 case NDR_NETR_LOGONSAMLOGONEX:
891 process_creds = false;
892 fn = "_netr_LogonSamLogonEx";
895 return NT_STATUS_INTERNAL_ERROR;
898 *r->out.authoritative = true; /* authoritative response */
900 switch (r->in.validation_level) {
902 r->out.validation->sam2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo2);
903 if (!r->out.validation->sam2) {
904 return NT_STATUS_NO_MEMORY;
908 r->out.validation->sam3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo3);
909 if (!r->out.validation->sam3) {
910 return NT_STATUS_NO_MEMORY;
914 DEBUG(0,("%s: bad validation_level value %d.\n",
915 fn, (int)r->in.validation_level));
916 return NT_STATUS_INVALID_INFO_CLASS;
919 switch (r->in.logon_level) {
920 case NetlogonInteractiveInformation:
921 case NetlogonServiceInformation:
922 case NetlogonInteractiveTransitiveInformation:
923 case NetlogonServiceTransitiveInformation:
924 nt_username = logon->password->identity_info.account_name.string;
925 nt_domain = logon->password->identity_info.domain_name.string;
926 nt_workstation = logon->password->identity_info.workstation.string;
928 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
930 case NetlogonNetworkInformation:
931 case NetlogonNetworkTransitiveInformation:
932 nt_username = logon->network->identity_info.account_name.string;
933 nt_domain = logon->network->identity_info.domain_name.string;
934 nt_workstation = logon->network->identity_info.workstation.string;
936 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
939 DEBUG(2,("SAM Logon: unsupported switch value\n"));
940 return NT_STATUS_INVALID_INFO_CLASS;
943 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
944 fstrcpy(current_user_info.smb_name, nt_username);
945 sub_set_smb_name(nt_username);
947 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
948 r->in.validation_level, nt_username));
950 status = NT_STATUS_OK;
952 switch (r->in.logon_level) {
953 case NetlogonNetworkInformation:
954 case NetlogonNetworkTransitiveInformation:
956 const char *wksname = nt_workstation;
958 status = make_auth_context_fixed(&auth_context,
959 logon->network->challenge);
960 if (!NT_STATUS_IS_OK(status)) {
964 /* For a network logon, the workstation name comes in with two
965 * backslashes in the front. Strip them if they are there. */
967 if (*wksname == '\\') wksname++;
968 if (*wksname == '\\') wksname++;
970 /* Standard challenge/response authenticaion */
971 if (!make_user_info_netlogon_network(&user_info,
972 nt_username, nt_domain,
974 logon->network->identity_info.parameter_control,
975 logon->network->lm.data,
976 logon->network->lm.length,
977 logon->network->nt.data,
978 logon->network->nt.length)) {
979 status = NT_STATUS_NO_MEMORY;
983 case NetlogonInteractiveInformation:
984 case NetlogonServiceInformation:
985 case NetlogonInteractiveTransitiveInformation:
986 case NetlogonServiceTransitiveInformation:
988 /* 'Interactive' authentication, supplies the password in its
989 MD4 form, encrypted with the session key. We will convert
990 this to challenge/response for the auth subsystem to chew
995 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
999 auth_context->get_ntlm_challenge(auth_context, chal);
1001 if (!make_user_info_netlogon_interactive(&user_info,
1002 nt_username, nt_domain,
1004 logon->password->identity_info.parameter_control,
1006 logon->password->lmpassword.hash,
1007 logon->password->ntpassword.hash,
1008 creds->session_key)) {
1009 status = NT_STATUS_NO_MEMORY;
1014 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1015 return NT_STATUS_INVALID_INFO_CLASS;
1018 if ( NT_STATUS_IS_OK(status) ) {
1019 status = auth_context->check_ntlm_password(auth_context,
1020 user_info, &server_info);
1023 (auth_context->free)(&auth_context);
1024 free_user_info(&user_info);
1026 DEBUG(5,("%s: check_password returned status %s\n",
1027 fn, nt_errstr(status)));
1029 /* Check account and password */
1031 if (!NT_STATUS_IS_OK(status)) {
1032 /* If we don't know what this domain is, we need to
1033 indicate that we are not authoritative. This
1034 allows the client to decide if it needs to try
1035 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
1036 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1037 && !strequal(nt_domain, get_global_sam_name())
1038 && !is_trusted_domain(nt_domain) )
1039 *r->out.authoritative = false; /* We are not authoritative */
1041 TALLOC_FREE(server_info);
1045 if (server_info->guest) {
1046 /* We don't like guest domain logons... */
1047 DEBUG(5,("%s: Attempted domain logon as GUEST "
1049 TALLOC_FREE(server_info);
1050 return NT_STATUS_LOGON_FAILURE;
1053 /* This is the point at which, if the login was successful, that
1054 the SAM Local Security Authority should record that the user is
1055 logged in to the domain. */
1057 if (process_creds) {
1058 /* Get the pipe session key from the creds. */
1059 memcpy(pipe_session_key, creds->session_key, 16);
1061 /* Get the pipe session key from the schannel. */
1062 if ((p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL)
1063 || (p->auth.a_u.schannel_auth == NULL)) {
1064 return NT_STATUS_INVALID_HANDLE;
1066 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->creds->session_key, 16);
1069 switch (r->in.validation_level) {
1071 status = serverinfo_to_SamInfo2(server_info, pipe_session_key, 16,
1072 r->out.validation->sam2);
1075 status = serverinfo_to_SamInfo3(server_info, pipe_session_key, 16,
1076 r->out.validation->sam3);
1080 TALLOC_FREE(server_info);
1085 /****************************************************************
1086 _netr_LogonSamLogonWithFlags
1087 ****************************************************************/
1089 NTSTATUS _netr_LogonSamLogonWithFlags(pipes_struct *p,
1090 struct netr_LogonSamLogonWithFlags *r)
1093 struct netlogon_creds_CredentialState *creds;
1094 struct netr_LogonSamLogonEx r2;
1095 struct netr_Authenticator return_authenticator;
1098 status = netr_creds_server_step_check(p, p->mem_ctx,
1099 r->in.computer_name,
1101 &return_authenticator,
1104 if (!NT_STATUS_IS_OK(status)) {
1108 r2.in.server_name = r->in.server_name;
1109 r2.in.computer_name = r->in.computer_name;
1110 r2.in.logon_level = r->in.logon_level;
1111 r2.in.logon = r->in.logon;
1112 r2.in.validation_level = r->in.validation_level;
1113 r2.in.flags = r->in.flags;
1114 r2.out.validation = r->out.validation;
1115 r2.out.authoritative = r->out.authoritative;
1116 r2.out.flags = r->out.flags;
1118 status = _netr_LogonSamLogon_base(p, &r2, creds);
1120 *r->out.return_authenticator = return_authenticator;
1125 /*************************************************************************
1127 *************************************************************************/
1129 NTSTATUS _netr_LogonSamLogon(pipes_struct *p,
1130 struct netr_LogonSamLogon *r)
1133 struct netr_LogonSamLogonWithFlags r2;
1136 r2.in.server_name = r->in.server_name;
1137 r2.in.computer_name = r->in.computer_name;
1138 r2.in.credential = r->in.credential;
1139 r2.in.logon_level = r->in.logon_level;
1140 r2.in.logon = r->in.logon;
1141 r2.in.validation_level = r->in.validation_level;
1142 r2.in.return_authenticator = r->in.return_authenticator;
1143 r2.in.flags = &flags;
1144 r2.out.validation = r->out.validation;
1145 r2.out.authoritative = r->out.authoritative;
1146 r2.out.flags = &flags;
1147 r2.out.return_authenticator = r->out.return_authenticator;
1149 status = _netr_LogonSamLogonWithFlags(p, &r2);
1154 /*************************************************************************
1155 _netr_LogonSamLogonEx
1156 - no credential chaining. Map into net sam logon.
1157 *************************************************************************/
1159 NTSTATUS _netr_LogonSamLogonEx(pipes_struct *p,
1160 struct netr_LogonSamLogonEx *r)
1163 struct netlogon_creds_CredentialState *creds = NULL;
1166 status = schannel_fetch_session_key(p->mem_ctx, r->in.computer_name, &creds);
1168 if (!NT_STATUS_IS_OK(status)) {
1172 /* Only allow this if the pipe is protected. */
1173 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1174 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1175 get_remote_machine_name() ));
1176 return NT_STATUS_INVALID_PARAMETER;
1179 status = _netr_LogonSamLogon_base(p, r, creds);
1185 /*************************************************************************
1187 *************************************************************************/
1188 #if 0 /* JERRY -- not correct */
1189 NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1190 DS_R_ENUM_DOM_TRUSTS *r_u)
1192 NTSTATUS status = NT_STATUS_OK;
1194 /* TODO: According to MSDN, the can only be executed against a
1195 DC or domain member running Windows 2000 or later. Need
1196 to test against a standalone 2k server and see what it
1197 does. A windows 2000 DC includes its own domain in the
1205 /****************************************************************
1206 ****************************************************************/
1208 WERROR _netr_LogonUasLogon(pipes_struct *p,
1209 struct netr_LogonUasLogon *r)
1211 p->rng_fault_state = true;
1212 return WERR_NOT_SUPPORTED;
1215 /****************************************************************
1216 ****************************************************************/
1218 WERROR _netr_LogonUasLogoff(pipes_struct *p,
1219 struct netr_LogonUasLogoff *r)
1221 p->rng_fault_state = true;
1222 return WERR_NOT_SUPPORTED;
1225 /****************************************************************
1226 ****************************************************************/
1228 NTSTATUS _netr_DatabaseDeltas(pipes_struct *p,
1229 struct netr_DatabaseDeltas *r)
1231 p->rng_fault_state = true;
1232 return NT_STATUS_NOT_IMPLEMENTED;
1235 /****************************************************************
1236 ****************************************************************/
1238 NTSTATUS _netr_DatabaseSync(pipes_struct *p,
1239 struct netr_DatabaseSync *r)
1241 p->rng_fault_state = true;
1242 return NT_STATUS_NOT_IMPLEMENTED;
1245 /****************************************************************
1246 ****************************************************************/
1248 NTSTATUS _netr_AccountDeltas(pipes_struct *p,
1249 struct netr_AccountDeltas *r)
1251 p->rng_fault_state = true;
1252 return NT_STATUS_NOT_IMPLEMENTED;
1255 /****************************************************************
1256 ****************************************************************/
1258 NTSTATUS _netr_AccountSync(pipes_struct *p,
1259 struct netr_AccountSync *r)
1261 p->rng_fault_state = true;
1262 return NT_STATUS_NOT_IMPLEMENTED;
1265 /****************************************************************
1266 ****************************************************************/
1268 WERROR _netr_GetDcName(pipes_struct *p,
1269 struct netr_GetDcName *r)
1271 p->rng_fault_state = true;
1272 return WERR_NOT_SUPPORTED;
1275 /****************************************************************
1276 ****************************************************************/
1278 WERROR _netr_GetAnyDCName(pipes_struct *p,
1279 struct netr_GetAnyDCName *r)
1281 p->rng_fault_state = true;
1282 return WERR_NOT_SUPPORTED;
1285 /****************************************************************
1286 ****************************************************************/
1288 NTSTATUS _netr_DatabaseSync2(pipes_struct *p,
1289 struct netr_DatabaseSync2 *r)
1291 p->rng_fault_state = true;
1292 return NT_STATUS_NOT_IMPLEMENTED;
1295 /****************************************************************
1296 ****************************************************************/
1298 NTSTATUS _netr_DatabaseRedo(pipes_struct *p,
1299 struct netr_DatabaseRedo *r)
1301 p->rng_fault_state = true;
1302 return NT_STATUS_NOT_IMPLEMENTED;
1305 /****************************************************************
1306 ****************************************************************/
1308 WERROR _netr_DsRGetDCName(pipes_struct *p,
1309 struct netr_DsRGetDCName *r)
1311 p->rng_fault_state = true;
1312 return WERR_NOT_SUPPORTED;
1315 /****************************************************************
1316 ****************************************************************/
1318 NTSTATUS _netr_LogonGetCapabilities(pipes_struct *p,
1319 struct netr_LogonGetCapabilities *r)
1321 return NT_STATUS_NOT_IMPLEMENTED;
1324 /****************************************************************
1325 ****************************************************************/
1327 WERROR _netr_NETRLOGONSETSERVICEBITS(pipes_struct *p,
1328 struct netr_NETRLOGONSETSERVICEBITS *r)
1330 p->rng_fault_state = true;
1331 return WERR_NOT_SUPPORTED;
1334 /****************************************************************
1335 ****************************************************************/
1337 WERROR _netr_LogonGetTrustRid(pipes_struct *p,
1338 struct netr_LogonGetTrustRid *r)
1340 p->rng_fault_state = true;
1341 return WERR_NOT_SUPPORTED;
1344 /****************************************************************
1345 ****************************************************************/
1347 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(pipes_struct *p,
1348 struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
1350 p->rng_fault_state = true;
1351 return WERR_NOT_SUPPORTED;
1354 /****************************************************************
1355 ****************************************************************/
1357 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(pipes_struct *p,
1358 struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
1360 p->rng_fault_state = true;
1361 return WERR_NOT_SUPPORTED;
1364 /****************************************************************
1365 ****************************************************************/
1367 WERROR _netr_DsRGetDCNameEx(pipes_struct *p,
1368 struct netr_DsRGetDCNameEx *r)
1370 p->rng_fault_state = true;
1371 return WERR_NOT_SUPPORTED;
1374 /****************************************************************
1375 ****************************************************************/
1377 WERROR _netr_DsRGetSiteName(pipes_struct *p,
1378 struct netr_DsRGetSiteName *r)
1380 p->rng_fault_state = true;
1381 return WERR_NOT_SUPPORTED;
1384 /****************************************************************
1385 ****************************************************************/
1387 NTSTATUS _netr_LogonGetDomainInfo(pipes_struct *p,
1388 struct netr_LogonGetDomainInfo *r)
1390 p->rng_fault_state = true;
1391 return NT_STATUS_NOT_IMPLEMENTED;
1394 /****************************************************************
1395 ****************************************************************/
1397 WERROR _netr_ServerPasswordGet(pipes_struct *p,
1398 struct netr_ServerPasswordGet *r)
1400 p->rng_fault_state = true;
1401 return WERR_NOT_SUPPORTED;
1404 /****************************************************************
1405 ****************************************************************/
1407 WERROR _netr_NETRLOGONSENDTOSAM(pipes_struct *p,
1408 struct netr_NETRLOGONSENDTOSAM *r)
1410 p->rng_fault_state = true;
1411 return WERR_NOT_SUPPORTED;
1414 /****************************************************************
1415 ****************************************************************/
1417 WERROR _netr_DsRAddressToSitenamesW(pipes_struct *p,
1418 struct netr_DsRAddressToSitenamesW *r)
1420 p->rng_fault_state = true;
1421 return WERR_NOT_SUPPORTED;
1424 /****************************************************************
1425 ****************************************************************/
1427 WERROR _netr_DsRGetDCNameEx2(pipes_struct *p,
1428 struct netr_DsRGetDCNameEx2 *r)
1430 p->rng_fault_state = true;
1431 return WERR_NOT_SUPPORTED;
1434 /****************************************************************
1435 ****************************************************************/
1437 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(pipes_struct *p,
1438 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
1440 p->rng_fault_state = true;
1441 return WERR_NOT_SUPPORTED;
1444 /****************************************************************
1445 ****************************************************************/
1447 WERROR _netr_NetrEnumerateTrustedDomainsEx(pipes_struct *p,
1448 struct netr_NetrEnumerateTrustedDomainsEx *r)
1450 p->rng_fault_state = true;
1451 return WERR_NOT_SUPPORTED;
1454 /****************************************************************
1455 ****************************************************************/
1457 WERROR _netr_DsRAddressToSitenamesExW(pipes_struct *p,
1458 struct netr_DsRAddressToSitenamesExW *r)
1460 p->rng_fault_state = true;
1461 return WERR_NOT_SUPPORTED;
1464 /****************************************************************
1465 ****************************************************************/
1467 WERROR _netr_DsrGetDcSiteCoverageW(pipes_struct *p,
1468 struct netr_DsrGetDcSiteCoverageW *r)
1470 p->rng_fault_state = true;
1471 return WERR_NOT_SUPPORTED;
1474 /****************************************************************
1475 ****************************************************************/
1477 WERROR _netr_DsrEnumerateDomainTrusts(pipes_struct *p,
1478 struct netr_DsrEnumerateDomainTrusts *r)
1480 p->rng_fault_state = true;
1481 return WERR_NOT_SUPPORTED;
1484 /****************************************************************
1485 ****************************************************************/
1487 WERROR _netr_DsrDeregisterDNSHostRecords(pipes_struct *p,
1488 struct netr_DsrDeregisterDNSHostRecords *r)
1490 p->rng_fault_state = true;
1491 return WERR_NOT_SUPPORTED;
1494 /****************************************************************
1495 ****************************************************************/
1497 NTSTATUS _netr_ServerTrustPasswordsGet(pipes_struct *p,
1498 struct netr_ServerTrustPasswordsGet *r)
1500 p->rng_fault_state = true;
1501 return NT_STATUS_NOT_IMPLEMENTED;
1504 /****************************************************************
1505 ****************************************************************/
1507 WERROR _netr_DsRGetForestTrustInformation(pipes_struct *p,
1508 struct netr_DsRGetForestTrustInformation *r)
1510 p->rng_fault_state = true;
1511 return WERR_NOT_SUPPORTED;
1514 /****************************************************************
1515 ****************************************************************/
1517 WERROR _netr_GetForestTrustInformation(pipes_struct *p,
1518 struct netr_GetForestTrustInformation *r)
1520 p->rng_fault_state = true;
1521 return WERR_NOT_SUPPORTED;
1524 /****************************************************************
1525 ****************************************************************/
1527 NTSTATUS _netr_ServerGetTrustInfo(pipes_struct *p,
1528 struct netr_ServerGetTrustInfo *r)
1530 p->rng_fault_state = true;
1531 return NT_STATUS_NOT_IMPLEMENTED;