Fix a bug that prevent core files from being created
[Samba.git] / source / rpc_server / srv_netlog_nt.c
blob0d6d80d6462a7369c8ce0e6b6b918221c7e0b84a
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Paul Ashton 1997.
7 * Copyright (C) Jeremy Allison 1998-2001.
8 * Copyright (C) Andrew Bartlett 2001.
9 * Copyright (C) Guenther Deschner 2008.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 /* This is the implementation of the netlogon pipe. */
27 #include "includes.h"
29 extern userdom_struct current_user_info;
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_RPC_SRV
34 /*************************************************************************
35 init_net_r_req_chal:
36 *************************************************************************/
38 static void init_net_r_req_chal(struct netr_Credential *r,
39 struct netr_Credential *srv_chal)
41 DEBUG(6,("init_net_r_req_chal: %d\n", __LINE__));
43 memcpy(r->data, srv_chal->data, sizeof(r->data));
46 /*******************************************************************
47 Inits a netr_NETLOGON_INFO_1 structure.
48 ********************************************************************/
50 static void init_netlogon_info1(struct netr_NETLOGON_INFO_1 *r,
51 uint32_t flags,
52 uint32_t pdc_connection_status)
54 r->flags = flags;
55 r->pdc_connection_status = pdc_connection_status;
58 /*******************************************************************
59 Inits a netr_NETLOGON_INFO_2 structure.
60 ********************************************************************/
62 static void init_netlogon_info2(struct netr_NETLOGON_INFO_2 *r,
63 uint32_t flags,
64 uint32_t pdc_connection_status,
65 const char *trusted_dc_name,
66 uint32_t tc_connection_status)
68 r->flags = flags;
69 r->pdc_connection_status = pdc_connection_status;
70 r->trusted_dc_name = trusted_dc_name;
71 r->tc_connection_status = tc_connection_status;
74 /*******************************************************************
75 Inits a netr_NETLOGON_INFO_3 structure.
76 ********************************************************************/
78 static void init_netlogon_info3(struct netr_NETLOGON_INFO_3 *r,
79 uint32_t flags,
80 uint32_t logon_attempts)
82 r->flags = flags;
83 r->logon_attempts = logon_attempts;
86 /*************************************************************************
87 _netr_LogonControl
88 *************************************************************************/
90 WERROR _netr_LogonControl(pipes_struct *p,
91 struct netr_LogonControl *r)
93 struct netr_NETLOGON_INFO_1 *info1;
94 uint32_t flags = 0x0;
95 uint32_t pdc_connection_status = W_ERROR_V(WERR_OK);
97 /* Setup the Logon Control response */
99 switch (r->in.level) {
100 case 1:
101 info1 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_1);
102 if (!info1) {
103 return WERR_NOMEM;
105 init_netlogon_info1(info1,
106 flags,
107 pdc_connection_status);
108 r->out.info->info1 = info1;
109 break;
110 default:
111 return WERR_UNKNOWN_LEVEL;
114 return WERR_OK;
117 /****************************************************************************
118 Send a message to smbd to do a sam synchronisation
119 **************************************************************************/
121 static void send_sync_message(void)
123 DEBUG(3, ("sending sam synchronisation message\n"));
124 message_send_all(smbd_messaging_context(), MSG_SMB_SAM_SYNC, NULL, 0,
125 NULL);
128 /*************************************************************************
129 _netr_LogonControl2
130 *************************************************************************/
132 WERROR _netr_LogonControl2(pipes_struct *p,
133 struct netr_LogonControl2 *r)
135 uint32 flags = 0x0;
136 uint32 pdc_connection_status = 0x0;
137 uint32 logon_attempts = 0x0;
138 uint32 tc_status;
139 fstring dc_name2;
140 const char *dc_name = NULL;
141 struct sockaddr_storage dc_ss;
142 const char *domain = NULL;
143 struct netr_NETLOGON_INFO_1 *info1;
144 struct netr_NETLOGON_INFO_2 *info2;
145 struct netr_NETLOGON_INFO_3 *info3;
147 tc_status = W_ERROR_V(WERR_NO_SUCH_DOMAIN);
149 switch (r->in.function_code) {
150 case NETLOGON_CONTROL_TC_QUERY:
151 domain = r->in.data->domain;
153 if ( !is_trusted_domain( domain ) )
154 break;
156 if ( !get_dc_name( domain, NULL, dc_name2, &dc_ss ) ) {
157 tc_status = W_ERROR_V(WERR_NO_LOGON_SERVERS);
158 break;
161 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
162 if (!dc_name) {
163 return WERR_NOMEM;
166 tc_status = W_ERROR_V(WERR_OK);
168 break;
170 case NETLOGON_CONTROL_REDISCOVER:
171 domain = r->in.data->domain;
173 if ( !is_trusted_domain( domain ) )
174 break;
176 if ( !get_dc_name( domain, NULL, dc_name2, &dc_ss ) ) {
177 tc_status = W_ERROR_V(WERR_NO_LOGON_SERVERS);
178 break;
181 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
182 if (!dc_name) {
183 return WERR_NOMEM;
186 tc_status = W_ERROR_V(WERR_OK);
188 break;
190 default:
191 /* no idea what this should be */
192 DEBUG(0,("_netr_LogonControl2: unimplemented function level [%d]\n",
193 r->in.function_code));
194 return WERR_UNKNOWN_LEVEL;
197 /* prepare the response */
199 switch (r->in.level) {
200 case 1:
201 info1 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_1);
202 W_ERROR_HAVE_NO_MEMORY(info1);
204 init_netlogon_info1(info1,
205 flags,
206 pdc_connection_status);
207 r->out.query->info1 = info1;
208 break;
209 case 2:
210 info2 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_2);
211 W_ERROR_HAVE_NO_MEMORY(info2);
213 init_netlogon_info2(info2,
214 flags,
215 pdc_connection_status,
216 dc_name,
217 tc_status);
218 r->out.query->info2 = info2;
219 break;
220 case 3:
221 info3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_NETLOGON_INFO_3);
222 W_ERROR_HAVE_NO_MEMORY(info3);
224 init_netlogon_info3(info3,
225 flags,
226 logon_attempts);
227 r->out.query->info3 = info3;
228 break;
229 default:
230 return WERR_UNKNOWN_LEVEL;
233 if (lp_server_role() == ROLE_DOMAIN_BDC) {
234 send_sync_message();
237 return WERR_OK;
240 /*************************************************************************
241 _netr_NetrEnumerateTrustedDomains
242 *************************************************************************/
244 WERROR _netr_NetrEnumerateTrustedDomains(pipes_struct *p,
245 struct netr_NetrEnumerateTrustedDomains *r)
247 struct netr_Blob trusted_domains_blob;
248 DATA_BLOB blob;
250 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
252 /* set up the Trusted Domain List response */
254 blob = data_blob_talloc_zero(p->mem_ctx, 2);
255 trusted_domains_blob.data = blob.data;
256 trusted_domains_blob.length = blob.length;
258 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
260 *r->out.trusted_domains_blob = trusted_domains_blob;
262 return WERR_OK;
265 /******************************************************************
266 gets a machine password entry. checks access rights of the host.
267 ******************************************************************/
269 static NTSTATUS get_md4pw(char *md4pw, const char *mach_acct, uint16 sec_chan_type)
271 struct samu *sampass = NULL;
272 const uint8 *pass;
273 bool ret;
274 uint32 acct_ctrl;
276 #if 0
277 char addr[INET6_ADDRSTRLEN];
280 * Currently this code is redundent as we already have a filter
281 * by hostname list. What this code really needs to do is to
282 * get a hosts allowed/hosts denied list from the SAM database
283 * on a per user basis, and make the access decision there.
284 * I will leave this code here for now as a reminder to implement
285 * this at a later date. JRA.
288 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
289 client_name(get_client_fd()),
290 client_addr(get_client_fd(),addr,sizeof(addr)))) {
291 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
292 return False;
294 #endif /* 0 */
296 if ( !(sampass = samu_new( NULL )) ) {
297 return NT_STATUS_NO_MEMORY;
300 /* JRA. This is ok as it is only used for generating the challenge. */
301 become_root();
302 ret = pdb_getsampwnam(sampass, mach_acct);
303 unbecome_root();
305 if (!ret) {
306 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
307 TALLOC_FREE(sampass);
308 return NT_STATUS_ACCESS_DENIED;
311 acct_ctrl = pdb_get_acct_ctrl(sampass);
312 if (acct_ctrl & ACB_DISABLED) {
313 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
314 TALLOC_FREE(sampass);
315 return NT_STATUS_ACCOUNT_DISABLED;
318 if (!(acct_ctrl & ACB_SVRTRUST) &&
319 !(acct_ctrl & ACB_WSTRUST) &&
320 !(acct_ctrl & ACB_DOMTRUST))
322 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
323 TALLOC_FREE(sampass);
324 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
327 switch (sec_chan_type) {
328 case SEC_CHAN_BDC:
329 if (!(acct_ctrl & ACB_SVRTRUST)) {
330 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
331 "but not a server trust account\n", mach_acct));
332 TALLOC_FREE(sampass);
333 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
335 break;
336 case SEC_CHAN_WKSTA:
337 if (!(acct_ctrl & ACB_WSTRUST)) {
338 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
339 "but not a workstation trust account\n", mach_acct));
340 TALLOC_FREE(sampass);
341 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
343 break;
344 case SEC_CHAN_DOMAIN:
345 if (!(acct_ctrl & ACB_DOMTRUST)) {
346 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
347 "but not a interdomain trust account\n", mach_acct));
348 TALLOC_FREE(sampass);
349 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
351 break;
352 default:
353 break;
356 if ((pass = pdb_get_nt_passwd(sampass)) == NULL) {
357 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
358 TALLOC_FREE(sampass);
359 return NT_STATUS_LOGON_FAILURE;
362 memcpy(md4pw, pass, 16);
363 dump_data(5, (uint8 *)md4pw, 16);
365 TALLOC_FREE(sampass);
367 return NT_STATUS_OK;
372 /*************************************************************************
373 _netr_ServerReqChallenge
374 *************************************************************************/
376 NTSTATUS _netr_ServerReqChallenge(pipes_struct *p,
377 struct netr_ServerReqChallenge *r)
379 if (!p->dc) {
380 p->dc = TALLOC_ZERO_P(p, struct dcinfo);
381 if (!p->dc) {
382 return NT_STATUS_NO_MEMORY;
384 } else {
385 DEBUG(10,("_netr_ServerReqChallenge: new challenge requested. Clearing old state.\n"));
386 ZERO_STRUCTP(p->dc);
389 fstrcpy(p->dc->remote_machine, r->in.computer_name);
391 /* Save the client challenge to the server. */
392 memcpy(p->dc->clnt_chal.data, r->in.credentials->data,
393 sizeof(r->in.credentials->data));
395 /* Create a server challenge for the client */
396 /* Set this to a random value. */
397 generate_random_buffer(p->dc->srv_chal.data, 8);
399 /* set up the LSA REQUEST CHALLENGE response */
400 init_net_r_req_chal(r->out.return_credentials, &p->dc->srv_chal);
402 p->dc->challenge_sent = True;
404 return NT_STATUS_OK;
407 /*************************************************************************
408 _netr_ServerAuthenticate
409 Create the initial credentials.
410 *************************************************************************/
412 NTSTATUS _netr_ServerAuthenticate(pipes_struct *p,
413 struct netr_ServerAuthenticate *r)
415 NTSTATUS status;
416 struct netr_Credential srv_chal_out;
418 if (!p->dc || !p->dc->challenge_sent) {
419 return NT_STATUS_ACCESS_DENIED;
422 status = get_md4pw((char *)p->dc->mach_pw,
423 r->in.account_name,
424 r->in.secure_channel_type);
425 if (!NT_STATUS_IS_OK(status)) {
426 DEBUG(0,("_netr_ServerAuthenticate: get_md4pw failed. Failed to "
427 "get password for machine account %s "
428 "from client %s: %s\n",
429 r->in.account_name,
430 r->in.computer_name,
431 nt_errstr(status) ));
432 /* always return NT_STATUS_ACCESS_DENIED */
433 return NT_STATUS_ACCESS_DENIED;
436 /* From the client / server challenges and md4 password, generate sess key */
437 creds_server_init(0, /* No neg flags. */
438 p->dc,
439 &p->dc->clnt_chal, /* Stored client chal. */
440 &p->dc->srv_chal, /* Stored server chal. */
441 p->dc->mach_pw,
442 &srv_chal_out);
444 /* Check client credentials are valid. */
445 if (!netlogon_creds_server_check(p->dc, r->in.credentials)) {
446 DEBUG(0,("_netr_ServerAuthenticate: netlogon_creds_server_check failed. Rejecting auth "
447 "request from client %s machine account %s\n",
448 r->in.computer_name,
449 r->in.account_name));
450 return NT_STATUS_ACCESS_DENIED;
453 fstrcpy(p->dc->mach_acct, r->in.account_name);
454 fstrcpy(p->dc->remote_machine, r->in.computer_name);
455 p->dc->authenticated = True;
457 /* set up the LSA AUTH response */
458 /* Return the server credentials. */
460 memcpy(r->out.return_credentials->data, &srv_chal_out.data,
461 sizeof(r->out.return_credentials->data));
463 return NT_STATUS_OK;
466 /*************************************************************************
467 _netr_ServerAuthenticate2
468 *************************************************************************/
470 NTSTATUS _netr_ServerAuthenticate2(pipes_struct *p,
471 struct netr_ServerAuthenticate2 *r)
473 NTSTATUS status;
474 uint32_t srv_flgs;
475 struct netr_Credential srv_chal_out;
477 /* According to Microsoft (see bugid #6099)
478 * Windows 7 looks at the negotiate_flags
479 * returned in this structure *even if the
480 * call fails with access denied ! So in order
481 * to allow Win7 to connect to a Samba NT style
482 * PDC we set the flags before we know if it's
483 * an error or not.
486 /* 0x000001ff */
487 srv_flgs = NETLOGON_NEG_ACCOUNT_LOCKOUT |
488 NETLOGON_NEG_PERSISTENT_SAMREPL |
489 NETLOGON_NEG_ARCFOUR |
490 NETLOGON_NEG_PROMOTION_COUNT |
491 NETLOGON_NEG_CHANGELOG_BDC |
492 NETLOGON_NEG_FULL_SYNC_REPL |
493 NETLOGON_NEG_MULTIPLE_SIDS |
494 NETLOGON_NEG_REDO |
495 NETLOGON_NEG_PASSWORD_CHANGE_REFUSAL;
497 if (lp_server_schannel() != false) {
498 srv_flgs |= NETLOGON_NEG_SCHANNEL;
501 *r->out.negotiate_flags = srv_flgs;
503 /* We use this as the key to store the creds: */
504 /* r->in.computer_name */
506 if (!p->dc || !p->dc->challenge_sent) {
507 DEBUG(0,("_netr_ServerAuthenticate2: no challenge sent to client %s\n",
508 r->in.computer_name));
509 return NT_STATUS_ACCESS_DENIED;
512 if ( (lp_server_schannel() == true) &&
513 ((*r->in.negotiate_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
515 /* schannel must be used, but client did not offer it. */
516 DEBUG(0,("_netr_ServerAuthenticate2: schannel required but client failed "
517 "to offer it. Client was %s\n",
518 r->in.account_name));
519 return NT_STATUS_ACCESS_DENIED;
522 status = get_md4pw((char *)p->dc->mach_pw,
523 r->in.account_name,
524 r->in.secure_channel_type);
525 if (!NT_STATUS_IS_OK(status)) {
526 DEBUG(0,("_netr_ServerAuthenticate2: failed to get machine password for "
527 "account %s: %s\n",
528 r->in.account_name, nt_errstr(status) ));
529 /* always return NT_STATUS_ACCESS_DENIED */
530 return NT_STATUS_ACCESS_DENIED;
533 /* From the client / server challenges and md4 password, generate sess key */
534 creds_server_init(*r->in.negotiate_flags,
535 p->dc,
536 &p->dc->clnt_chal, /* Stored client chal. */
537 &p->dc->srv_chal, /* Stored server chal. */
538 p->dc->mach_pw,
539 &srv_chal_out);
541 /* Check client credentials are valid. */
542 if (!netlogon_creds_server_check(p->dc, r->in.credentials)) {
543 DEBUG(0,("_netr_ServerAuthenticate2: netlogon_creds_server_check failed. Rejecting auth "
544 "request from client %s machine account %s\n",
545 r->in.computer_name,
546 r->in.account_name));
547 return NT_STATUS_ACCESS_DENIED;
549 /* set up the LSA AUTH 2 response */
550 memcpy(r->out.return_credentials->data, &srv_chal_out.data,
551 sizeof(r->out.return_credentials->data));
553 fstrcpy(p->dc->mach_acct, r->in.account_name);
554 fstrcpy(p->dc->remote_machine, r->in.computer_name);
555 fstrcpy(p->dc->domain, lp_workgroup() );
557 p->dc->authenticated = True;
559 /* Store off the state so we can continue after client disconnect. */
560 become_root();
561 secrets_store_schannel_session_info(p->mem_ctx,
562 r->in.computer_name,
563 p->dc);
564 unbecome_root();
566 return NT_STATUS_OK;
569 /*************************************************************************
570 _netr_ServerPasswordSet
571 *************************************************************************/
573 NTSTATUS _netr_ServerPasswordSet(pipes_struct *p,
574 struct netr_ServerPasswordSet *r)
576 NTSTATUS status = NT_STATUS_OK;
577 fstring remote_machine;
578 struct samu *sampass=NULL;
579 bool ret = False;
580 unsigned char pwd[16];
581 int i;
582 uint32 acct_ctrl;
583 struct netr_Authenticator cred_out;
584 const uchar *old_pw;
586 DEBUG(5,("_netr_ServerPasswordSet: %d\n", __LINE__));
588 /* We need the remote machine name for the creds lookup. */
589 fstrcpy(remote_machine, r->in.computer_name);
591 if ( (lp_server_schannel() == True) && (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) ) {
592 /* 'server schannel = yes' should enforce use of
593 schannel, the client did offer it in auth2, but
594 obviously did not use it. */
595 DEBUG(0,("_netr_ServerPasswordSet: client %s not using schannel for netlogon\n",
596 remote_machine ));
597 return NT_STATUS_ACCESS_DENIED;
600 if (!p->dc) {
601 /* Restore the saved state of the netlogon creds. */
602 become_root();
603 ret = secrets_restore_schannel_session_info(p, remote_machine,
604 &p->dc);
605 unbecome_root();
606 if (!ret) {
607 return NT_STATUS_INVALID_HANDLE;
611 if (!p->dc || !p->dc->authenticated) {
612 return NT_STATUS_INVALID_HANDLE;
615 DEBUG(3,("_netr_ServerPasswordSet: Server Password Set by remote machine:[%s] on account [%s]\n",
616 remote_machine, p->dc->mach_acct));
618 /* Step the creds chain forward. */
619 if (!netlogon_creds_server_step(p->dc, r->in.credential, &cred_out)) {
620 DEBUG(2,("_netr_ServerPasswordSet: netlogon_creds_server_step failed. Rejecting auth "
621 "request from client %s machine account %s\n",
622 remote_machine, p->dc->mach_acct ));
623 return NT_STATUS_INVALID_PARAMETER;
626 /* We must store the creds state after an update. */
627 sampass = samu_new( NULL );
628 if (!sampass) {
629 return NT_STATUS_NO_MEMORY;
632 become_root();
633 secrets_store_schannel_session_info(p, remote_machine, p->dc);
634 ret = pdb_getsampwnam(sampass, p->dc->mach_acct);
635 unbecome_root();
637 if (!ret) {
638 TALLOC_FREE(sampass);
639 return NT_STATUS_ACCESS_DENIED;
642 /* Ensure the account exists and is a machine account. */
644 acct_ctrl = pdb_get_acct_ctrl(sampass);
646 if (!(acct_ctrl & ACB_WSTRUST ||
647 acct_ctrl & ACB_SVRTRUST ||
648 acct_ctrl & ACB_DOMTRUST)) {
649 TALLOC_FREE(sampass);
650 return NT_STATUS_NO_SUCH_USER;
653 if (pdb_get_acct_ctrl(sampass) & ACB_DISABLED) {
654 TALLOC_FREE(sampass);
655 return NT_STATUS_ACCOUNT_DISABLED;
658 /* Woah - what does this to to the credential chain ? JRA */
659 cred_hash3(pwd, r->in.new_password->hash, p->dc->sess_key, 0);
661 DEBUG(100,("_netr_ServerPasswordSet: new given value was :\n"));
662 for(i = 0; i < sizeof(pwd); i++)
663 DEBUG(100,("%02X ", pwd[i]));
664 DEBUG(100,("\n"));
666 old_pw = pdb_get_nt_passwd(sampass);
668 if (old_pw && memcmp(pwd, old_pw, 16) == 0) {
669 /* Avoid backend modificiations and other fun if the
670 client changed the password to the *same thing* */
672 ret = True;
673 } else {
675 /* LM password should be NULL for machines */
676 if (!pdb_set_lanman_passwd(sampass, NULL, PDB_CHANGED)) {
677 TALLOC_FREE(sampass);
678 return NT_STATUS_NO_MEMORY;
681 if (!pdb_set_nt_passwd(sampass, pwd, PDB_CHANGED)) {
682 TALLOC_FREE(sampass);
683 return NT_STATUS_NO_MEMORY;
686 if (!pdb_set_pass_last_set_time(sampass, time(NULL), PDB_CHANGED)) {
687 TALLOC_FREE(sampass);
688 /* Not quite sure what this one qualifies as, but this will do */
689 return NT_STATUS_UNSUCCESSFUL;
692 become_root();
693 status = pdb_update_sam_account(sampass);
694 unbecome_root();
697 /* set up the LSA Server Password Set response */
699 memcpy(r->out.return_authenticator, &cred_out,
700 sizeof(*(r->out.return_authenticator)));
702 TALLOC_FREE(sampass);
703 return status;
706 /*************************************************************************
707 _netr_LogonSamLogoff
708 *************************************************************************/
710 NTSTATUS _netr_LogonSamLogoff(pipes_struct *p,
711 struct netr_LogonSamLogoff *r)
713 if ( (lp_server_schannel() == True) && (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) ) {
714 /* 'server schannel = yes' should enforce use of
715 schannel, the client did offer it in auth2, but
716 obviously did not use it. */
717 DEBUG(0,("_netr_LogonSamLogoff: client %s not using schannel for netlogon\n",
718 get_remote_machine_name() ));
719 return NT_STATUS_ACCESS_DENIED;
723 /* Using the remote machine name for the creds store: */
724 /* r->in.computer_name */
726 if (!p->dc) {
727 /* Restore the saved state of the netlogon creds. */
728 bool ret;
730 become_root();
731 ret = secrets_restore_schannel_session_info(
732 p, r->in.computer_name, &p->dc);
733 unbecome_root();
734 if (!ret) {
735 return NT_STATUS_INVALID_HANDLE;
739 if (!p->dc || !p->dc->authenticated) {
740 return NT_STATUS_INVALID_HANDLE;
743 /* checks and updates credentials. creates reply credentials */
744 if (!netlogon_creds_server_step(p->dc, r->in.credential, r->out.return_authenticator)) {
745 DEBUG(2,("_netr_LogonSamLogoff: netlogon_creds_server_step failed. Rejecting auth "
746 "request from client %s machine account %s\n",
747 r->in.computer_name, p->dc->mach_acct ));
748 return NT_STATUS_INVALID_PARAMETER;
751 /* We must store the creds state after an update. */
752 become_root();
753 secrets_store_schannel_session_info(p, r->in.computer_name, p->dc);
754 unbecome_root();
756 return NT_STATUS_OK;
759 /*************************************************************************
760 _netr_LogonSamLogon
761 *************************************************************************/
763 NTSTATUS _netr_LogonSamLogon(pipes_struct *p,
764 struct netr_LogonSamLogon *r)
766 NTSTATUS status = NT_STATUS_OK;
767 struct netr_SamInfo3 *sam3 = NULL;
768 union netr_LogonInfo *logon = r->in.logon;
769 fstring nt_username, nt_domain, nt_workstation;
770 auth_usersupplied_info *user_info = NULL;
771 auth_serversupplied_info *server_info = NULL;
772 struct auth_context *auth_context = NULL;
773 uint8_t pipe_session_key[16];
774 bool process_creds = true;
776 switch (p->hdr_req.opnum) {
777 case NDR_NETR_LOGONSAMLOGON:
778 process_creds = true;
779 break;
780 case NDR_NETR_LOGONSAMLOGONEX:
781 default:
782 process_creds = false;
785 if ( (lp_server_schannel() == True) && (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) ) {
786 /* 'server schannel = yes' should enforce use of
787 schannel, the client did offer it in auth2, but
788 obviously did not use it. */
789 DEBUG(0,("_netr_LogonSamLogon: client %s not using schannel for netlogon\n",
790 get_remote_machine_name() ));
791 return NT_STATUS_ACCESS_DENIED;
794 sam3 = TALLOC_ZERO_P(p->mem_ctx, struct netr_SamInfo3);
795 if (!sam3) {
796 return NT_STATUS_NO_MEMORY;
799 /* store the user information, if there is any. */
800 r->out.validation->sam3 = sam3;
801 *r->out.authoritative = true; /* authoritative response */
802 if (r->in.validation_level != 2 && r->in.validation_level != 3) {
803 DEBUG(0,("_netr_LogonSamLogon: bad validation_level value %d.\n",
804 (int)r->in.validation_level));
805 return NT_STATUS_ACCESS_DENIED;
808 if (process_creds) {
810 /* Get the remote machine name for the creds store. */
811 /* Note this is the remote machine this request is coming from (member server),
812 not neccessarily the workstation name the user is logging onto.
815 if (!p->dc) {
816 /* Restore the saved state of the netlogon creds. */
817 bool ret;
819 become_root();
820 ret = secrets_restore_schannel_session_info(
821 p, r->in.computer_name, &p->dc);
822 unbecome_root();
823 if (!ret) {
824 return NT_STATUS_INVALID_HANDLE;
828 if (!p->dc || !p->dc->authenticated) {
829 return NT_STATUS_INVALID_HANDLE;
832 /* checks and updates credentials. creates reply credentials */
833 if (!netlogon_creds_server_step(p->dc, r->in.credential, r->out.return_authenticator)) {
834 DEBUG(2,("_netr_LogonSamLogon: creds_server_step failed. Rejecting auth "
835 "request from client %s machine account %s\n",
836 r->in.computer_name, p->dc->mach_acct ));
837 return NT_STATUS_INVALID_PARAMETER;
840 /* We must store the creds state after an update. */
841 become_root();
842 secrets_store_schannel_session_info(p, r->in.computer_name, p->dc);
843 unbecome_root();
846 switch (r->in.logon_level) {
847 case INTERACTIVE_LOGON_TYPE:
848 fstrcpy(nt_username,
849 logon->password->identity_info.account_name.string);
850 fstrcpy(nt_domain,
851 logon->password->identity_info.domain_name.string);
852 fstrcpy(nt_workstation,
853 logon->password->identity_info.workstation.string);
855 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
856 break;
857 case NET_LOGON_TYPE:
858 fstrcpy(nt_username,
859 logon->network->identity_info.account_name.string);
860 fstrcpy(nt_domain,
861 logon->network->identity_info.domain_name.string);
862 fstrcpy(nt_workstation,
863 logon->network->identity_info.workstation.string);
865 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
866 break;
867 default:
868 DEBUG(2,("SAM Logon: unsupported switch value\n"));
869 return NT_STATUS_INVALID_INFO_CLASS;
870 } /* end switch */
872 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
873 fstrcpy(current_user_info.smb_name, nt_username);
874 sub_set_smb_name(nt_username);
876 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
877 r->in.validation_level, nt_username));
879 status = NT_STATUS_OK;
881 switch (r->in.logon_level) {
882 case NET_LOGON_TYPE:
884 const char *wksname = nt_workstation;
886 status = make_auth_context_fixed(&auth_context,
887 logon->network->challenge);
888 if (!NT_STATUS_IS_OK(status)) {
889 return status;
892 /* For a network logon, the workstation name comes in with two
893 * backslashes in the front. Strip them if they are there. */
895 if (*wksname == '\\') wksname++;
896 if (*wksname == '\\') wksname++;
898 /* Standard challenge/response authenticaion */
899 if (!make_user_info_netlogon_network(&user_info,
900 nt_username, nt_domain,
901 wksname,
902 logon->network->identity_info.parameter_control,
903 logon->network->lm.data,
904 logon->network->lm.length,
905 logon->network->nt.data,
906 logon->network->nt.length)) {
907 status = NT_STATUS_NO_MEMORY;
909 break;
911 case INTERACTIVE_LOGON_TYPE:
912 /* 'Interactive' authentication, supplies the password in its
913 MD4 form, encrypted with the session key. We will convert
914 this to challenge/response for the auth subsystem to chew
915 on */
917 const uint8 *chal;
919 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
920 return status;
923 chal = auth_context->get_ntlm_challenge(auth_context);
925 if (!make_user_info_netlogon_interactive(&user_info,
926 nt_username, nt_domain,
927 nt_workstation,
928 logon->password->identity_info.parameter_control,
929 chal,
930 logon->password->lmpassword.hash,
931 logon->password->ntpassword.hash,
932 p->dc->sess_key)) {
933 status = NT_STATUS_NO_MEMORY;
935 break;
937 default:
938 DEBUG(2,("SAM Logon: unsupported switch value\n"));
939 return NT_STATUS_INVALID_INFO_CLASS;
940 } /* end switch */
942 if ( NT_STATUS_IS_OK(status) ) {
943 status = auth_context->check_ntlm_password(auth_context,
944 user_info, &server_info);
947 (auth_context->free)(&auth_context);
948 free_user_info(&user_info);
950 DEBUG(5,("_netr_LogonSamLogon: check_password returned status %s\n",
951 nt_errstr(status)));
953 /* Check account and password */
955 if (!NT_STATUS_IS_OK(status)) {
956 /* If we don't know what this domain is, we need to
957 indicate that we are not authoritative. This
958 allows the client to decide if it needs to try
959 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
960 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
961 && !strequal(nt_domain, get_global_sam_name())
962 && !is_trusted_domain(nt_domain) )
963 *r->out.authoritative = false; /* We are not authoritative */
965 TALLOC_FREE(server_info);
966 return status;
969 if (server_info->guest) {
970 /* We don't like guest domain logons... */
971 DEBUG(5,("_netr_LogonSamLogon: Attempted domain logon as GUEST "
972 "denied.\n"));
973 TALLOC_FREE(server_info);
974 return NT_STATUS_LOGON_FAILURE;
977 /* This is the point at which, if the login was successful, that
978 the SAM Local Security Authority should record that the user is
979 logged in to the domain. */
981 if (process_creds) {
982 /* Get the pipe session key from the creds. */
983 memcpy(pipe_session_key, p->dc->sess_key, 16);
984 } else {
985 /* Get the pipe session key from the schannel. */
986 if ((p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL)
987 || (p->auth.a_u.schannel_auth == NULL)) {
988 return NT_STATUS_INVALID_HANDLE;
990 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->sess_key, 16);
993 status = serverinfo_to_SamInfo3(server_info, pipe_session_key, sam3);
994 TALLOC_FREE(server_info);
995 return status;
998 /*************************************************************************
999 _netr_LogonSamLogonEx
1000 - no credential chaining. Map into net sam logon.
1001 *************************************************************************/
1003 NTSTATUS _netr_LogonSamLogonEx(pipes_struct *p,
1004 struct netr_LogonSamLogonEx *r)
1006 struct netr_LogonSamLogon q;
1008 /* Only allow this if the pipe is protected. */
1009 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1010 DEBUG(0,("_net_sam_logon_ex: client %s not using schannel for netlogon\n",
1011 get_remote_machine_name() ));
1012 return NT_STATUS_INVALID_PARAMETER;
1015 q.in.server_name = r->in.server_name;
1016 q.in.computer_name = r->in.computer_name;
1017 q.in.logon_level = r->in.logon_level;
1018 q.in.logon = r->in.logon;
1019 q.in.validation_level = r->in.validation_level;
1020 /* we do not handle the flags */
1021 /* = r->in.flags; */
1023 q.out.validation = r->out.validation;
1024 q.out.authoritative = r->out.authoritative;
1025 /* we do not handle the flags */
1026 /* = r->out.flags; */
1028 return _netr_LogonSamLogon(p, &q);
1031 /*************************************************************************
1032 _ds_enum_dom_trusts
1033 *************************************************************************/
1034 #if 0 /* JERRY -- not correct */
1035 NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1036 DS_R_ENUM_DOM_TRUSTS *r_u)
1038 NTSTATUS status = NT_STATUS_OK;
1040 /* TODO: According to MSDN, the can only be executed against a
1041 DC or domain member running Windows 2000 or later. Need
1042 to test against a standalone 2k server and see what it
1043 does. A windows 2000 DC includes its own domain in the
1044 list. --jerry */
1046 return status;
1048 #endif /* JERRY */
1051 /****************************************************************
1052 ****************************************************************/
1054 WERROR _netr_LogonUasLogon(pipes_struct *p,
1055 struct netr_LogonUasLogon *r)
1057 p->rng_fault_state = true;
1058 return WERR_NOT_SUPPORTED;
1061 /****************************************************************
1062 ****************************************************************/
1064 WERROR _netr_LogonUasLogoff(pipes_struct *p,
1065 struct netr_LogonUasLogoff *r)
1067 p->rng_fault_state = true;
1068 return WERR_NOT_SUPPORTED;
1071 /****************************************************************
1072 ****************************************************************/
1074 NTSTATUS _netr_DatabaseDeltas(pipes_struct *p,
1075 struct netr_DatabaseDeltas *r)
1077 p->rng_fault_state = true;
1078 return NT_STATUS_NOT_IMPLEMENTED;
1081 /****************************************************************
1082 ****************************************************************/
1084 NTSTATUS _netr_DatabaseSync(pipes_struct *p,
1085 struct netr_DatabaseSync *r)
1087 p->rng_fault_state = true;
1088 return NT_STATUS_NOT_IMPLEMENTED;
1091 /****************************************************************
1092 ****************************************************************/
1094 NTSTATUS _netr_AccountDeltas(pipes_struct *p,
1095 struct netr_AccountDeltas *r)
1097 p->rng_fault_state = true;
1098 return NT_STATUS_NOT_IMPLEMENTED;
1101 /****************************************************************
1102 ****************************************************************/
1104 NTSTATUS _netr_AccountSync(pipes_struct *p,
1105 struct netr_AccountSync *r)
1107 p->rng_fault_state = true;
1108 return NT_STATUS_NOT_IMPLEMENTED;
1111 /****************************************************************
1112 ****************************************************************/
1114 WERROR _netr_GetDcName(pipes_struct *p,
1115 struct netr_GetDcName *r)
1117 p->rng_fault_state = true;
1118 return WERR_NOT_SUPPORTED;
1121 /****************************************************************
1122 ****************************************************************/
1124 WERROR _netr_GetAnyDCName(pipes_struct *p,
1125 struct netr_GetAnyDCName *r)
1127 p->rng_fault_state = true;
1128 return WERR_NOT_SUPPORTED;
1131 /****************************************************************
1132 ****************************************************************/
1134 NTSTATUS _netr_DatabaseSync2(pipes_struct *p,
1135 struct netr_DatabaseSync2 *r)
1137 p->rng_fault_state = true;
1138 return NT_STATUS_NOT_IMPLEMENTED;
1141 /****************************************************************
1142 ****************************************************************/
1144 NTSTATUS _netr_DatabaseRedo(pipes_struct *p,
1145 struct netr_DatabaseRedo *r)
1147 p->rng_fault_state = true;
1148 return NT_STATUS_NOT_IMPLEMENTED;
1151 /****************************************************************
1152 ****************************************************************/
1154 WERROR _netr_LogonControl2Ex(pipes_struct *p,
1155 struct netr_LogonControl2Ex *r)
1157 p->rng_fault_state = true;
1158 return WERR_NOT_SUPPORTED;
1161 /****************************************************************
1162 ****************************************************************/
1164 WERROR _netr_DsRGetDCName(pipes_struct *p,
1165 struct netr_DsRGetDCName *r)
1167 p->rng_fault_state = true;
1168 return WERR_NOT_SUPPORTED;
1171 /****************************************************************
1172 ****************************************************************/
1174 NTSTATUS _netr_LogonGetCapabilities(pipes_struct *p,
1175 struct netr_LogonGetCapabilities *r)
1177 return NT_STATUS_NOT_IMPLEMENTED;
1180 /****************************************************************
1181 ****************************************************************/
1183 WERROR _netr_NETRLOGONSETSERVICEBITS(pipes_struct *p,
1184 struct netr_NETRLOGONSETSERVICEBITS *r)
1186 p->rng_fault_state = true;
1187 return WERR_NOT_SUPPORTED;
1190 /****************************************************************
1191 ****************************************************************/
1193 WERROR _netr_LogonGetTrustRid(pipes_struct *p,
1194 struct netr_LogonGetTrustRid *r)
1196 p->rng_fault_state = true;
1197 return WERR_NOT_SUPPORTED;
1200 /****************************************************************
1201 ****************************************************************/
1203 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(pipes_struct *p,
1204 struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
1206 p->rng_fault_state = true;
1207 return WERR_NOT_SUPPORTED;
1210 /****************************************************************
1211 ****************************************************************/
1213 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(pipes_struct *p,
1214 struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
1216 p->rng_fault_state = true;
1217 return WERR_NOT_SUPPORTED;
1220 /****************************************************************
1221 ****************************************************************/
1223 NTSTATUS _netr_ServerAuthenticate3(pipes_struct *p,
1224 struct netr_ServerAuthenticate3 *r)
1226 p->rng_fault_state = true;
1227 return NT_STATUS_NOT_IMPLEMENTED;
1230 /****************************************************************
1231 ****************************************************************/
1233 WERROR _netr_DsRGetDCNameEx(pipes_struct *p,
1234 struct netr_DsRGetDCNameEx *r)
1236 p->rng_fault_state = true;
1237 return WERR_NOT_SUPPORTED;
1240 /****************************************************************
1241 ****************************************************************/
1243 WERROR _netr_DsRGetSiteName(pipes_struct *p,
1244 struct netr_DsRGetSiteName *r)
1246 p->rng_fault_state = true;
1247 return WERR_NOT_SUPPORTED;
1250 /****************************************************************
1251 ****************************************************************/
1253 NTSTATUS _netr_LogonGetDomainInfo(pipes_struct *p,
1254 struct netr_LogonGetDomainInfo *r)
1256 p->rng_fault_state = true;
1257 return NT_STATUS_NOT_IMPLEMENTED;
1260 /****************************************************************
1261 ****************************************************************/
1263 NTSTATUS _netr_ServerPasswordSet2(pipes_struct *p,
1264 struct netr_ServerPasswordSet2 *r)
1266 p->rng_fault_state = true;
1267 return NT_STATUS_NOT_IMPLEMENTED;
1270 /****************************************************************
1271 ****************************************************************/
1273 WERROR _netr_ServerPasswordGet(pipes_struct *p,
1274 struct netr_ServerPasswordGet *r)
1276 p->rng_fault_state = true;
1277 return WERR_NOT_SUPPORTED;
1280 /****************************************************************
1281 ****************************************************************/
1283 WERROR _netr_NETRLOGONSENDTOSAM(pipes_struct *p,
1284 struct netr_NETRLOGONSENDTOSAM *r)
1286 p->rng_fault_state = true;
1287 return WERR_NOT_SUPPORTED;
1290 /****************************************************************
1291 ****************************************************************/
1293 WERROR _netr_DsRAddressToSitenamesW(pipes_struct *p,
1294 struct netr_DsRAddressToSitenamesW *r)
1296 p->rng_fault_state = true;
1297 return WERR_NOT_SUPPORTED;
1300 /****************************************************************
1301 ****************************************************************/
1303 WERROR _netr_DsRGetDCNameEx2(pipes_struct *p,
1304 struct netr_DsRGetDCNameEx2 *r)
1306 p->rng_fault_state = true;
1307 return WERR_NOT_SUPPORTED;
1310 /****************************************************************
1311 ****************************************************************/
1313 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(pipes_struct *p,
1314 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
1316 p->rng_fault_state = true;
1317 return WERR_NOT_SUPPORTED;
1320 /****************************************************************
1321 ****************************************************************/
1323 WERROR _netr_NetrEnumerateTrustedDomainsEx(pipes_struct *p,
1324 struct netr_NetrEnumerateTrustedDomainsEx *r)
1326 p->rng_fault_state = true;
1327 return WERR_NOT_SUPPORTED;
1330 /****************************************************************
1331 ****************************************************************/
1333 WERROR _netr_DsRAddressToSitenamesExW(pipes_struct *p,
1334 struct netr_DsRAddressToSitenamesExW *r)
1336 p->rng_fault_state = true;
1337 return WERR_NOT_SUPPORTED;
1340 /****************************************************************
1341 ****************************************************************/
1343 WERROR _netr_DsrGetDcSiteCoverageW(pipes_struct *p,
1344 struct netr_DsrGetDcSiteCoverageW *r)
1346 p->rng_fault_state = true;
1347 return WERR_NOT_SUPPORTED;
1350 /****************************************************************
1351 ****************************************************************/
1353 WERROR _netr_DsrEnumerateDomainTrusts(pipes_struct *p,
1354 struct netr_DsrEnumerateDomainTrusts *r)
1356 p->rng_fault_state = true;
1357 return WERR_NOT_SUPPORTED;
1360 /****************************************************************
1361 ****************************************************************/
1363 WERROR _netr_DsrDeregisterDNSHostRecords(pipes_struct *p,
1364 struct netr_DsrDeregisterDNSHostRecords *r)
1366 p->rng_fault_state = true;
1367 return WERR_NOT_SUPPORTED;
1370 /****************************************************************
1371 ****************************************************************/
1373 NTSTATUS _netr_ServerTrustPasswordsGet(pipes_struct *p,
1374 struct netr_ServerTrustPasswordsGet *r)
1376 p->rng_fault_state = true;
1377 return NT_STATUS_NOT_IMPLEMENTED;
1380 /****************************************************************
1381 ****************************************************************/
1383 WERROR _netr_DsRGetForestTrustInformation(pipes_struct *p,
1384 struct netr_DsRGetForestTrustInformation *r)
1386 p->rng_fault_state = true;
1387 return WERR_NOT_SUPPORTED;
1390 /****************************************************************
1391 ****************************************************************/
1393 WERROR _netr_GetForestTrustInformation(pipes_struct *p,
1394 struct netr_GetForestTrustInformation *r)
1396 p->rng_fault_state = true;
1397 return WERR_NOT_SUPPORTED;
1400 /****************************************************************
1401 ****************************************************************/
1403 NTSTATUS _netr_LogonSamLogonWithFlags(pipes_struct *p,
1404 struct netr_LogonSamLogonWithFlags *r)
1406 p->rng_fault_state = true;
1407 return NT_STATUS_NOT_IMPLEMENTED;
1410 /****************************************************************
1411 ****************************************************************/
1413 WERROR _netr_NETRSERVERGETTRUSTINFO(pipes_struct *p,
1414 struct netr_NETRSERVERGETTRUSTINFO *r)
1416 p->rng_fault_state = true;
1417 return WERR_NOT_SUPPORTED;