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.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* This is the implementation of the netlogon pipe. */
29 extern userdom_struct current_user_info;
32 #define DBGC_CLASS DBGC_RPC_SRV
34 /*************************************************************************
36 *************************************************************************/
38 static void init_net_r_req_chal(NET_R_REQ_CHAL *r_c,
39 DOM_CHAL *srv_chal, NTSTATUS status)
41 DEBUG(6,("init_net_r_req_chal: %d\n", __LINE__));
42 memcpy(r_c->srv_chal.data, srv_chal->data, sizeof(srv_chal->data));
46 /*************************************************************************
47 error messages cropping up when using nltest.exe...
48 *************************************************************************/
50 #define ERROR_NO_SUCH_DOMAIN 0x54b
51 #define ERROR_NO_LOGON_SERVERS 0x51f
54 /*************************************************************************
56 *************************************************************************/
58 NTSTATUS _net_logon_ctrl(pipes_struct *p, NET_Q_LOGON_CTRL *q_u,
59 NET_R_LOGON_CTRL *r_u)
62 uint32 pdc_connection_status = 0x00; /* Maybe a win32 error code? */
64 /* Setup the Logon Control response */
66 init_net_r_logon_ctrl(r_u, q_u->query_level, flags,
67 pdc_connection_status);
72 /****************************************************************************
73 Send a message to smbd to do a sam synchronisation
74 **************************************************************************/
76 static void send_sync_message(void)
80 tdb = tdb_open_log(lock_path("connections.tdb"), 0,
81 TDB_DEFAULT, O_RDONLY, 0);
84 DEBUG(3, ("send_sync_message(): failed to open connections "
89 DEBUG(3, ("sending sam synchronisation message\n"));
91 message_send_all(tdb, MSG_SMB_SAM_SYNC, NULL, 0, False, NULL);
96 /*************************************************************************
97 net_reply_logon_ctrl2:
98 *************************************************************************/
100 NTSTATUS _net_logon_ctrl2(pipes_struct *p, NET_Q_LOGON_CTRL2 *q_u, NET_R_LOGON_CTRL2 *r_u)
103 uint32 pdc_connection_status = 0x0;
104 uint32 logon_attempts = 0x0;
106 fstring servername, domain, dc_name, dc_name2;
107 struct in_addr dc_ip;
109 /* this should be \\global_myname() */
110 unistr2_to_ascii(servername, &q_u->uni_server_name, sizeof(servername));
112 r_u->status = NT_STATUS_OK;
114 tc_status = ERROR_NO_SUCH_DOMAIN;
115 fstrcpy( dc_name, "" );
117 switch ( q_u->function_code ) {
118 case NETLOGON_CONTROL_TC_QUERY:
119 unistr2_to_ascii(domain, &q_u->info.info6.domain, sizeof(domain));
121 if ( !is_trusted_domain( domain ) )
124 if ( !get_dc_name( domain, NULL, dc_name2, &dc_ip ) ) {
125 tc_status = ERROR_NO_LOGON_SERVERS;
129 fstr_sprintf( dc_name, "\\\\%s", dc_name2 );
131 tc_status = NO_ERROR;
135 case NETLOGON_CONTROL_REDISCOVER:
136 unistr2_to_ascii(domain, &q_u->info.info6.domain, sizeof(domain));
138 if ( !is_trusted_domain( domain ) )
141 if ( !get_dc_name( domain, NULL, dc_name2, &dc_ip ) ) {
142 tc_status = ERROR_NO_LOGON_SERVERS;
146 fstr_sprintf( dc_name, "\\\\%s", dc_name2 );
148 tc_status = NO_ERROR;
153 /* no idea what this should be */
154 DEBUG(0,("_net_logon_ctrl2: unimplemented function level [%d]\n",
155 q_u->function_code));
158 /* prepare the response */
160 init_net_r_logon_ctrl2( r_u, q_u->query_level, flags,
161 pdc_connection_status, logon_attempts, tc_status, dc_name );
163 if (lp_server_role() == ROLE_DOMAIN_BDC)
169 /*************************************************************************
170 net_reply_trust_dom_list:
171 *************************************************************************/
173 NTSTATUS _net_trust_dom_list(pipes_struct *p, NET_Q_TRUST_DOM_LIST *q_u, NET_R_TRUST_DOM_LIST *r_u)
175 const char *trusted_domain = "test_domain";
176 uint32 num_trust_domains = 1;
178 DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
180 /* set up the Trusted Domain List response */
181 init_r_trust_dom(r_u, num_trust_domains, trusted_domain);
183 DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
188 /***********************************************************************************
189 init_net_r_srv_pwset:
190 ***********************************************************************************/
192 static void init_net_r_srv_pwset(NET_R_SRV_PWSET *r_s,
193 DOM_CRED *srv_cred, NTSTATUS status)
195 DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
197 memcpy(&r_s->srv_cred, srv_cred, sizeof(r_s->srv_cred));
198 r_s->status = status;
200 DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
203 /******************************************************************
204 gets a machine password entry. checks access rights of the host.
205 ******************************************************************/
207 static BOOL get_md4pw(char *md4pw, char *mach_acct)
209 struct samu *sampass = NULL;
216 * Currently this code is redundent as we already have a filter
217 * by hostname list. What this code really needs to do is to
218 * get a hosts allowed/hosts denied list from the SAM database
219 * on a per user basis, and make the access decision there.
220 * I will leave this code here for now as a reminder to implement
221 * this at a later date. JRA.
224 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
225 client_name(), client_addr()))
227 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
232 if ( !(sampass = samu_new( NULL )) ) {
236 /* JRA. This is ok as it is only used for generating the challenge. */
238 ret=pdb_getsampwnam(sampass, mach_acct);
242 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
243 TALLOC_FREE(sampass);
247 acct_ctrl = pdb_get_acct_ctrl(sampass);
248 if (!(acct_ctrl & ACB_DISABLED) &&
249 ((acct_ctrl & ACB_DOMTRUST) ||
250 (acct_ctrl & ACB_WSTRUST) ||
251 (acct_ctrl & ACB_SVRTRUST)) &&
252 ((pass=pdb_get_nt_passwd(sampass)) != NULL)) {
253 memcpy(md4pw, pass, 16);
254 dump_data(5, md4pw, 16);
255 TALLOC_FREE(sampass);
259 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
260 TALLOC_FREE(sampass);
265 /*************************************************************************
267 *************************************************************************/
269 NTSTATUS _net_req_chal(pipes_struct *p, NET_Q_REQ_CHAL *q_u, NET_R_REQ_CHAL *r_u)
272 p->dc = TALLOC_ZERO_P(p->pipe_state_mem_ctx, struct dcinfo);
274 return NT_STATUS_NO_MEMORY;
277 DEBUG(10,("_net_req_chal: new challenge requested. Clearing old state.\n"));
281 rpcstr_pull(p->dc->remote_machine,
282 q_u->uni_logon_clnt.buffer,
283 sizeof(fstring),q_u->uni_logon_clnt.uni_str_len*2,0);
285 /* Save the client challenge to the server. */
286 memcpy(p->dc->clnt_chal.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
288 /* Create a server challenge for the client */
289 /* Set this to a random value. */
290 generate_random_buffer(p->dc->srv_chal.data, 8);
292 /* set up the LSA REQUEST CHALLENGE response */
293 init_net_r_req_chal(r_u, &p->dc->srv_chal, NT_STATUS_OK);
295 p->dc->challenge_sent = True;
300 /*************************************************************************
302 *************************************************************************/
304 static void init_net_r_auth(NET_R_AUTH *r_a, DOM_CHAL *resp_cred, NTSTATUS status)
306 memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
307 r_a->status = status;
310 /*************************************************************************
311 _net_auth. Create the initial credentials.
312 *************************************************************************/
314 NTSTATUS _net_auth(pipes_struct *p, NET_Q_AUTH *q_u, NET_R_AUTH *r_u)
317 fstring remote_machine;
318 DOM_CHAL srv_chal_out;
320 if (!p->dc || !p->dc->challenge_sent) {
321 return NT_STATUS_ACCESS_DENIED;
324 rpcstr_pull(mach_acct, q_u->clnt_id.uni_acct_name.buffer,sizeof(fstring),
325 q_u->clnt_id.uni_acct_name.uni_str_len*2,0);
326 rpcstr_pull(remote_machine, q_u->clnt_id.uni_comp_name.buffer,sizeof(fstring),
327 q_u->clnt_id.uni_comp_name.uni_str_len*2,0);
329 if (!get_md4pw((char *)p->dc->mach_pw, mach_acct)) {
330 DEBUG(0,("_net_auth: creds_server_check failed. Failed to "
331 "get pasword for machine account %s "
333 mach_acct, remote_machine ));
334 return NT_STATUS_ACCESS_DENIED;
337 /* From the client / server challenges and md4 password, generate sess key */
338 creds_server_init(0, /* No neg flags. */
340 &p->dc->clnt_chal, /* Stored client chal. */
341 &p->dc->srv_chal, /* Stored server chal. */
345 /* Check client credentials are valid. */
346 if (!creds_server_check(p->dc, &q_u->clnt_chal)) {
347 DEBUG(0,("_net_auth: creds_server_check failed. Rejecting auth "
348 "request from client %s machine account %s\n",
349 remote_machine, mach_acct ));
350 return NT_STATUS_ACCESS_DENIED;
353 fstrcpy(p->dc->mach_acct, mach_acct);
354 fstrcpy(p->dc->remote_machine, remote_machine);
355 p->dc->authenticated = True;
357 /* set up the LSA AUTH response */
358 /* Return the server credentials. */
359 init_net_r_auth(r_u, &srv_chal_out, NT_STATUS_OK);
364 /*************************************************************************
366 *************************************************************************/
368 static void init_net_r_auth_2(NET_R_AUTH_2 *r_a,
369 DOM_CHAL *resp_cred, NEG_FLAGS *flgs, NTSTATUS status)
371 memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
372 memcpy(&r_a->srv_flgs, flgs, sizeof(r_a->srv_flgs));
373 r_a->status = status;
376 /*************************************************************************
378 *************************************************************************/
380 NTSTATUS _net_auth_2(pipes_struct *p, NET_Q_AUTH_2 *q_u, NET_R_AUTH_2 *r_u)
384 fstring remote_machine;
385 DOM_CHAL srv_chal_out;
387 rpcstr_pull(mach_acct, q_u->clnt_id.uni_acct_name.buffer,sizeof(fstring),
388 q_u->clnt_id.uni_acct_name.uni_str_len*2,0);
390 /* We use this as the key to store the creds. */
391 rpcstr_pull(remote_machine, q_u->clnt_id.uni_comp_name.buffer,sizeof(fstring),
392 q_u->clnt_id.uni_comp_name.uni_str_len*2,0);
394 if (!p->dc || !p->dc->challenge_sent) {
395 DEBUG(0,("_net_auth2: no challenge sent to client %s\n",
397 return NT_STATUS_ACCESS_DENIED;
400 if ( (lp_server_schannel() == True) &&
401 ((q_u->clnt_flgs.neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
403 /* schannel must be used, but client did not offer it. */
404 DEBUG(0,("_net_auth2: schannel required but client failed "
405 "to offer it. Client was %s\n",
407 return NT_STATUS_ACCESS_DENIED;
410 if (!get_md4pw((char *)p->dc->mach_pw, mach_acct)) {
411 DEBUG(0,("_net_auth2: failed to get machine password for "
414 return NT_STATUS_ACCESS_DENIED;
417 /* From the client / server challenges and md4 password, generate sess key */
418 creds_server_init(q_u->clnt_flgs.neg_flags,
420 &p->dc->clnt_chal, /* Stored client chal. */
421 &p->dc->srv_chal, /* Stored server chal. */
425 /* Check client credentials are valid. */
426 if (!creds_server_check(p->dc, &q_u->clnt_chal)) {
427 DEBUG(0,("_net_auth2: creds_server_check failed. Rejecting auth "
428 "request from client %s machine account %s\n",
429 remote_machine, mach_acct ));
430 return NT_STATUS_ACCESS_DENIED;
433 srv_flgs.neg_flags = 0x000001ff;
435 if (lp_server_schannel() != False) {
436 srv_flgs.neg_flags |= NETLOGON_NEG_SCHANNEL;
439 /* set up the LSA AUTH 2 response */
440 init_net_r_auth_2(r_u, &srv_chal_out, &srv_flgs, NT_STATUS_OK);
442 fstrcpy(p->dc->mach_acct, mach_acct);
443 fstrcpy(p->dc->remote_machine, remote_machine);
444 fstrcpy(p->dc->domain, lp_workgroup() );
446 p->dc->authenticated = True;
448 /* Store off the state so we can continue after client disconnect. */
450 secrets_store_schannel_session_info(p->mem_ctx,
458 /*************************************************************************
460 *************************************************************************/
462 NTSTATUS _net_srv_pwset(pipes_struct *p, NET_Q_SRV_PWSET *q_u, NET_R_SRV_PWSET *r_u)
464 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
465 fstring remote_machine;
466 struct samu *sampass=NULL;
468 unsigned char pwd[16];
474 DEBUG(5,("_net_srv_pwset: %d\n", __LINE__));
476 /* We need the remote machine name for the creds lookup. */
477 rpcstr_pull(remote_machine,q_u->clnt_id.login.uni_comp_name.buffer,
478 sizeof(remote_machine),q_u->clnt_id.login.uni_comp_name.uni_str_len*2,0);
480 if ( (lp_server_schannel() == True) && (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) ) {
481 /* 'server schannel = yes' should enforce use of
482 schannel, the client did offer it in auth2, but
483 obviously did not use it. */
484 DEBUG(0,("_net_srv_pwset: client %s not using schannel for netlogon\n",
486 return NT_STATUS_ACCESS_DENIED;
490 /* Restore the saved state of the netlogon creds. */
492 ret = secrets_restore_schannel_session_info(p->pipe_state_mem_ctx,
497 return NT_STATUS_INVALID_HANDLE;
501 if (!p->dc || !p->dc->authenticated) {
502 return NT_STATUS_INVALID_HANDLE;
505 DEBUG(3,("_net_srv_pwset: Server Password Set by remote machine:[%s] on account [%s]\n",
506 remote_machine, p->dc->mach_acct));
508 /* Step the creds chain forward. */
509 if (!creds_server_step(p->dc, &q_u->clnt_id.cred, &cred_out)) {
510 DEBUG(2,("_net_srv_pwset: creds_server_step failed. Rejecting auth "
511 "request from client %s machine account %s\n",
512 remote_machine, p->dc->mach_acct ));
513 return NT_STATUS_INVALID_PARAMETER;
516 /* We must store the creds state after an update. */
518 secrets_store_schannel_session_info(p->pipe_state_mem_ctx,
521 if ( (sampass = samu_new( NULL )) != NULL ) {
522 ret = pdb_getsampwnam(sampass, p->dc->mach_acct);
527 return NT_STATUS_NO_MEMORY;
529 /* Ensure the account exists and is a machine account. */
531 acct_ctrl = pdb_get_acct_ctrl(sampass);
534 && (acct_ctrl & ACB_WSTRUST ||
535 acct_ctrl & ACB_SVRTRUST ||
536 acct_ctrl & ACB_DOMTRUST))) {
537 TALLOC_FREE(sampass);
538 return NT_STATUS_NO_SUCH_USER;
541 if (pdb_get_acct_ctrl(sampass) & ACB_DISABLED) {
542 TALLOC_FREE(sampass);
543 return NT_STATUS_ACCOUNT_DISABLED;
546 /* Woah - what does this to to the credential chain ? JRA */
547 cred_hash3( pwd, q_u->pwd, p->dc->sess_key, 0);
549 DEBUG(100,("Server password set : new given value was :\n"));
550 for(i = 0; i < sizeof(pwd); i++)
551 DEBUG(100,("%02X ", pwd[i]));
554 old_pw = pdb_get_nt_passwd(sampass);
556 if (old_pw && memcmp(pwd, old_pw, 16) == 0) {
557 /* Avoid backend modificiations and other fun if the
558 client changed the password to the *same thing* */
563 /* LM password should be NULL for machines */
564 if (!pdb_set_lanman_passwd(sampass, NULL, PDB_CHANGED)) {
565 TALLOC_FREE(sampass);
566 return NT_STATUS_NO_MEMORY;
569 if (!pdb_set_nt_passwd(sampass, pwd, PDB_CHANGED)) {
570 TALLOC_FREE(sampass);
571 return NT_STATUS_NO_MEMORY;
574 if (!pdb_set_pass_changed_now(sampass)) {
575 TALLOC_FREE(sampass);
576 /* Not quite sure what this one qualifies as, but this will do */
577 return NT_STATUS_UNSUCCESSFUL;
581 r_u->status = pdb_update_sam_account (sampass);
585 /* set up the LSA Server Password Set response */
586 init_net_r_srv_pwset(r_u, &cred_out, status);
588 TALLOC_FREE(sampass);
592 /*************************************************************************
594 *************************************************************************/
596 NTSTATUS _net_sam_logoff(pipes_struct *p, NET_Q_SAM_LOGOFF *q_u, NET_R_SAM_LOGOFF *r_u)
598 fstring remote_machine;
600 if ( (lp_server_schannel() == True) && (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) ) {
601 /* 'server schannel = yes' should enforce use of
602 schannel, the client did offer it in auth2, but
603 obviously did not use it. */
604 DEBUG(0,("_net_sam_logoff: client %s not using schannel for netlogon\n",
605 get_remote_machine_name() ));
606 return NT_STATUS_ACCESS_DENIED;
610 if (!get_valid_user_struct(p->vuid))
611 return NT_STATUS_NO_SUCH_USER;
613 /* Get the remote machine name for the creds store. */
614 rpcstr_pull(remote_machine,q_u->sam_id.client.login.uni_comp_name.buffer,
615 sizeof(remote_machine),q_u->sam_id.client.login.uni_comp_name.uni_str_len*2,0);
618 /* Restore the saved state of the netlogon creds. */
622 ret = secrets_restore_schannel_session_info(p->pipe_state_mem_ctx,
627 return NT_STATUS_INVALID_HANDLE;
631 if (!p->dc || !p->dc->authenticated) {
632 return NT_STATUS_INVALID_HANDLE;
635 r_u->buffer_creds = 1; /* yes, we have valid server credentials */
637 /* checks and updates credentials. creates reply credentials */
638 if (!creds_server_step(p->dc, &q_u->sam_id.client.cred, &r_u->srv_creds)) {
639 DEBUG(2,("_net_sam_logoff: creds_server_step failed. Rejecting auth "
640 "request from client %s machine account %s\n",
641 remote_machine, p->dc->mach_acct ));
642 return NT_STATUS_INVALID_PARAMETER;
645 /* We must store the creds state after an update. */
647 secrets_store_schannel_session_info(p->pipe_state_mem_ctx,
652 r_u->status = NT_STATUS_OK;
656 /*******************************************************************
657 gets a domain user's groups from their already-calculated NT_USER_TOKEN
658 ********************************************************************/
660 static NTSTATUS nt_token_to_group_list(TALLOC_CTX *mem_ctx,
661 const DOM_SID *domain_sid,
664 int *numgroups, DOM_GID **pgids)
671 for (i=0; i<num_sids; i++) {
673 if (!sid_peek_check_rid(domain_sid, &sids[i], &gid.g_rid)) {
676 gid.attr = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
678 ADD_TO_ARRAY(mem_ctx, DOM_GID, gid, pgids, numgroups);
679 if (*pgids == NULL) {
680 return NT_STATUS_NO_MEMORY;
686 /*************************************************************************
688 *************************************************************************/
690 static NTSTATUS _net_sam_logon_internal(pipes_struct *p,
691 NET_Q_SAM_LOGON *q_u,
692 NET_R_SAM_LOGON *r_u,
695 NTSTATUS status = NT_STATUS_OK;
696 NET_USER_INFO_3 *usr_info = NULL;
697 NET_ID_INFO_CTR *ctr = q_u->sam_id.ctr;
698 UNISTR2 *uni_samlogon_user = NULL;
699 UNISTR2 *uni_samlogon_domain = NULL;
700 UNISTR2 *uni_samlogon_workstation = NULL;
701 fstring nt_username, nt_domain, nt_workstation;
702 auth_usersupplied_info *user_info = NULL;
703 auth_serversupplied_info *server_info = NULL;
705 struct auth_context *auth_context = NULL;
707 if ( (lp_server_schannel() == True) && (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) ) {
708 /* 'server schannel = yes' should enforce use of
709 schannel, the client did offer it in auth2, but
710 obviously did not use it. */
711 DEBUG(0,("_net_sam_logon_internal: client %s not using schannel for netlogon\n",
712 get_remote_machine_name() ));
713 return NT_STATUS_ACCESS_DENIED;
716 usr_info = TALLOC_P(p->mem_ctx, NET_USER_INFO_3);
718 return NT_STATUS_NO_MEMORY;
721 ZERO_STRUCTP(usr_info);
723 /* store the user information, if there is any. */
724 r_u->user = usr_info;
725 r_u->auth_resp = 1; /* authoritative response */
726 if (q_u->validation_level != 2 && q_u->validation_level != 3) {
727 DEBUG(0,("_net_sam_logon: bad validation_level value %d.\n", (int)q_u->validation_level ));
728 return NT_STATUS_ACCESS_DENIED;
730 /* We handle the return of USER_INFO_2 instead of 3 in the parse return. Sucks, I know... */
731 r_u->switch_value = q_u->validation_level; /* indicates type of validation user info */
732 r_u->buffer_creds = 1; /* Ensure we always return server creds. */
734 if (!get_valid_user_struct(p->vuid))
735 return NT_STATUS_NO_SUCH_USER;
738 fstring remote_machine;
740 /* Get the remote machine name for the creds store. */
741 /* Note this is the remote machine this request is coming from (member server),
742 not neccessarily the workstation name the user is logging onto.
744 rpcstr_pull(remote_machine,q_u->sam_id.client.login.uni_comp_name.buffer,
745 sizeof(remote_machine),q_u->sam_id.client.login.uni_comp_name.uni_str_len*2,0);
748 /* Restore the saved state of the netlogon creds. */
752 ret = secrets_restore_schannel_session_info(p->pipe_state_mem_ctx,
757 return NT_STATUS_INVALID_HANDLE;
761 if (!p->dc || !p->dc->authenticated) {
762 return NT_STATUS_INVALID_HANDLE;
765 /* checks and updates credentials. creates reply credentials */
766 if (!creds_server_step(p->dc, &q_u->sam_id.client.cred, &r_u->srv_creds)) {
767 DEBUG(2,("_net_sam_logon: creds_server_step failed. Rejecting auth "
768 "request from client %s machine account %s\n",
769 remote_machine, p->dc->mach_acct ));
770 return NT_STATUS_INVALID_PARAMETER;
773 /* We must store the creds state after an update. */
775 secrets_store_schannel_session_info(p->pipe_state_mem_ctx,
781 switch (q_u->sam_id.logon_level) {
782 case INTERACTIVE_LOGON_TYPE:
783 uni_samlogon_user = &ctr->auth.id1.uni_user_name;
784 uni_samlogon_domain = &ctr->auth.id1.uni_domain_name;
786 uni_samlogon_workstation = &ctr->auth.id1.uni_wksta_name;
788 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
791 uni_samlogon_user = &ctr->auth.id2.uni_user_name;
792 uni_samlogon_domain = &ctr->auth.id2.uni_domain_name;
793 uni_samlogon_workstation = &ctr->auth.id2.uni_wksta_name;
795 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
798 DEBUG(2,("SAM Logon: unsupported switch value\n"));
799 return NT_STATUS_INVALID_INFO_CLASS;
802 rpcstr_pull(nt_username,uni_samlogon_user->buffer,sizeof(nt_username),uni_samlogon_user->uni_str_len*2,0);
803 rpcstr_pull(nt_domain,uni_samlogon_domain->buffer,sizeof(nt_domain),uni_samlogon_domain->uni_str_len*2,0);
804 rpcstr_pull(nt_workstation,uni_samlogon_workstation->buffer,sizeof(nt_workstation),uni_samlogon_workstation->uni_str_len*2,0);
806 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
807 fstrcpy(current_user_info.smb_name, nt_username);
808 sub_set_smb_name(nt_username);
810 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n", q_u->sam_id.ctr->switch_value, nt_username));
812 status = NT_STATUS_OK;
814 switch (ctr->switch_value) {
817 const char *wksname = nt_workstation;
819 if (!NT_STATUS_IS_OK(status = make_auth_context_fixed(&auth_context, ctr->auth.id2.lm_chal))) {
823 /* For a network logon, the workstation name comes in with two
824 * backslashes in the front. Strip them if they are there. */
826 if (*wksname == '\\') wksname++;
827 if (*wksname == '\\') wksname++;
829 /* Standard challenge/response authenticaion */
830 if (!make_user_info_netlogon_network(&user_info,
831 nt_username, nt_domain,
833 ctr->auth.id2.param_ctrl,
834 ctr->auth.id2.lm_chal_resp.buffer,
835 ctr->auth.id2.lm_chal_resp.str_str_len,
836 ctr->auth.id2.nt_chal_resp.buffer,
837 ctr->auth.id2.nt_chal_resp.str_str_len)) {
838 status = NT_STATUS_NO_MEMORY;
842 case INTERACTIVE_LOGON_TYPE:
843 /* 'Interactive' authentication, supplies the password in its
844 MD4 form, encrypted with the session key. We will convert
845 this to challenge/response for the auth subsystem to chew
850 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
854 chal = auth_context->get_ntlm_challenge(auth_context);
856 if (!make_user_info_netlogon_interactive(&user_info,
857 nt_username, nt_domain,
859 ctr->auth.id1.param_ctrl,
861 ctr->auth.id1.lm_owf.data,
862 ctr->auth.id1.nt_owf.data,
864 status = NT_STATUS_NO_MEMORY;
869 DEBUG(2,("SAM Logon: unsupported switch value\n"));
870 return NT_STATUS_INVALID_INFO_CLASS;
873 if ( NT_STATUS_IS_OK(status) ) {
874 status = auth_context->check_ntlm_password(auth_context,
875 user_info, &server_info);
878 (auth_context->free)(&auth_context);
879 free_user_info(&user_info);
881 DEBUG(5, ("_net_sam_logon: check_password returned status %s\n",
884 /* Check account and password */
886 if (!NT_STATUS_IS_OK(status)) {
887 /* If we don't know what this domain is, we need to
888 indicate that we are not authoritative. This
889 allows the client to decide if it needs to try
890 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
891 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
892 && !strequal(nt_domain, get_global_sam_name())
893 && !is_trusted_domain(nt_domain) )
894 r_u->auth_resp = 0; /* We are not authoritative */
896 TALLOC_FREE(server_info);
900 if (server_info->guest) {
901 /* We don't like guest domain logons... */
902 DEBUG(5,("_net_sam_logon: Attempted domain logon as GUEST "
904 TALLOC_FREE(server_info);
905 return NT_STATUS_LOGON_FAILURE;
908 /* This is the point at which, if the login was successful, that
909 the SAM Local Security Authority should record that the user is
910 logged in to the domain. */
913 DOM_GID *gids = NULL;
914 const DOM_SID *user_sid = NULL;
915 const DOM_SID *group_sid = NULL;
917 uint32 user_rid, group_rid;
921 fstring user_sid_string;
922 fstring group_sid_string;
923 unsigned char user_session_key[16];
924 unsigned char lm_session_key[16];
925 unsigned char pipe_session_key[16];
927 sampw = server_info->sam_account;
929 /* set up pointer indicating user/password failed to be
931 usr_info->ptr_user_info = 0;
933 user_sid = pdb_get_user_sid(sampw);
934 group_sid = pdb_get_group_sid(sampw);
936 sid_copy(&domain_sid, user_sid);
937 sid_split_rid(&domain_sid, &user_rid);
939 if (!sid_peek_check_rid(&domain_sid, group_sid, &group_rid)) {
940 DEBUG(1, ("_net_sam_logon: user %s\\%s has user sid "
941 "%s\n but group sid %s.\n"
942 "The conflicting domain portions are not "
943 "supported for NETLOGON calls\n",
944 pdb_get_domain(sampw),
945 pdb_get_username(sampw),
946 sid_to_string(user_sid_string, user_sid),
947 sid_to_string(group_sid_string, group_sid)));
948 return NT_STATUS_UNSUCCESSFUL;
952 if(server_info->login_server) {
953 pstrcpy(my_name, server_info->login_server);
955 pstrcpy(my_name, global_myname());
958 status = nt_token_to_group_list(p->mem_ctx, &domain_sid,
959 server_info->num_sids,
963 if (!NT_STATUS_IS_OK(status)) {
967 if (server_info->user_session_key.length) {
968 memcpy(user_session_key,
969 server_info->user_session_key.data,
970 MIN(sizeof(user_session_key),
971 server_info->user_session_key.length));
973 /* Get the pipe session key from the creds. */
974 memcpy(pipe_session_key, p->dc->sess_key, 16);
976 /* Get the pipe session key from the schannel. */
977 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL || p->auth.a_u.schannel_auth == NULL) {
978 return NT_STATUS_INVALID_HANDLE;
980 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->sess_key, 16);
982 SamOEMhash(user_session_key, pipe_session_key, 16);
983 memset(pipe_session_key, '\0', 16);
985 if (server_info->lm_session_key.length) {
986 memcpy(lm_session_key,
987 server_info->lm_session_key.data,
988 MIN(sizeof(lm_session_key),
989 server_info->lm_session_key.length));
991 /* Get the pipe session key from the creds. */
992 memcpy(pipe_session_key, p->dc->sess_key, 16);
994 /* Get the pipe session key from the schannel. */
995 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL || p->auth.a_u.schannel_auth == NULL) {
996 return NT_STATUS_INVALID_HANDLE;
998 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->sess_key, 16);
1000 SamOEMhash(lm_session_key, pipe_session_key, 16);
1001 memset(pipe_session_key, '\0', 16);
1004 init_net_user_info3(p->mem_ctx, usr_info,
1007 pdb_get_username(sampw),
1008 pdb_get_fullname(sampw),
1009 pdb_get_homedir(sampw),
1010 pdb_get_dir_drive(sampw),
1011 pdb_get_logon_script(sampw),
1012 pdb_get_profile_path(sampw),
1013 pdb_get_logon_time(sampw),
1016 pdb_get_pass_last_set_time(sampw),
1017 pdb_get_pass_can_change_time(sampw),
1018 pdb_get_pass_must_change_time(sampw),
1020 0, /* logon_count */
1021 0, /* bad_pw_count */
1022 num_gids, /* uint32 num_groups */
1023 gids , /* DOM_GID *gids */
1024 0x20 , /* uint32 user_flgs (?) */
1025 server_info->user_session_key.length ? user_session_key : NULL,
1026 server_info->lm_session_key.length ? lm_session_key : NULL,
1027 my_name , /* char *logon_srv */
1028 pdb_get_domain(sampw),
1029 &domain_sid); /* DOM_SID *dom_sid */
1030 ZERO_STRUCT(user_session_key);
1031 ZERO_STRUCT(lm_session_key);
1033 TALLOC_FREE(server_info);
1037 /*************************************************************************
1039 *************************************************************************/
1041 NTSTATUS _net_sam_logon(pipes_struct *p, NET_Q_SAM_LOGON *q_u, NET_R_SAM_LOGON *r_u)
1043 return _net_sam_logon_internal(p, q_u, r_u, True);
1046 /*************************************************************************
1047 _net_sam_logon_ex - no credential chaining. Map into net sam logon.
1048 *************************************************************************/
1050 NTSTATUS _net_sam_logon_ex(pipes_struct *p, NET_Q_SAM_LOGON_EX *q_u, NET_R_SAM_LOGON_EX *r_u)
1058 /* Only allow this if the pipe is protected. */
1059 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1060 DEBUG(0,("_net_sam_logon_ex: client %s not using schannel for netlogon\n",
1061 get_remote_machine_name() ));
1062 return NT_STATUS_INVALID_PARAMETER;
1065 /* Map a NET_Q_SAM_LOGON_EX to NET_Q_SAM_LOGON. */
1066 q.validation_level = q_u->validation_level;
1068 /* Map a DOM_SAM_INFO_EX into a DOM_SAM_INFO with no creds. */
1069 q.sam_id.client.login = q_u->sam_id.client;
1070 q.sam_id.logon_level = q_u->sam_id.logon_level;
1071 q.sam_id.ctr = q_u->sam_id.ctr;
1073 r_u->status = _net_sam_logon_internal(p, &q, &r, False);
1075 if (!NT_STATUS_IS_OK(r_u->status)) {
1079 /* Map the NET_R_SAM_LOGON to NET_R_SAM_LOGON_EX. */
1080 r_u->switch_value = r.switch_value;
1082 r_u->auth_resp = r.auth_resp;
1083 r_u->flags = 0; /* FIXME ! */
1087 /*************************************************************************
1089 *************************************************************************/
1090 #if 0 /* JERRY -- not correct */
1091 NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1092 DS_R_ENUM_DOM_TRUSTS *r_u)
1094 NTSTATUS status = NT_STATUS_OK;
1096 /* TODO: According to MSDN, the can only be executed against a
1097 DC or domain member running Windows 2000 or later. Need
1098 to test against a standalone 2k server and see what it
1099 does. A windows 2000 DC includes its own domain in the