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 * Copyirht (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 pstring global_myname;
31 /*************************************************************************
33 *************************************************************************/
35 static void init_net_r_req_chal(NET_R_REQ_CHAL *r_c,
36 DOM_CHAL *srv_chal, NTSTATUS status)
38 DEBUG(6,("init_net_r_req_chal: %d\n", __LINE__));
39 memcpy(r_c->srv_chal.data, srv_chal->data, sizeof(srv_chal->data));
43 /*************************************************************************
44 error messages cropping up when using nltest.exe...
45 *************************************************************************/
47 #define ERROR_NO_SUCH_DOMAIN 0x54b
48 #define ERROR_NO_LOGON_SERVERS 0x51f
50 /*************************************************************************
52 *************************************************************************/
54 /* Some flag values reverse engineered from NLTEST.EXE */
56 #define LOGON_CTRL_IN_SYNC 0x00
57 #define LOGON_CTRL_REPL_NEEDED 0x01
58 #define LOGON_CTRL_REPL_IN_PROGRESS 0x02
60 NTSTATUS _net_logon_ctrl(pipes_struct *p, NET_Q_LOGON_CTRL *q_u,
61 NET_R_LOGON_CTRL *r_u)
64 uint32 pdc_connection_status = 0x00; /* Maybe a win32 error code? */
66 /* Setup the Logon Control response */
68 init_net_r_logon_ctrl(r_u, q_u->query_level, flags,
69 pdc_connection_status);
74 /****************************************************************************
75 Send a message to smbd to do a sam synchronisation
76 **************************************************************************/
77 static void send_sync_message(void)
81 tdb = tdb_open_log(lock_path("connections.tdb"), 0,
82 TDB_DEFAULT, O_RDONLY, 0);
85 DEBUG(3, ("send_sync_message(): failed to open connections "
90 DEBUG(3, ("sending sam synchronisation message\n"));
92 message_send_all(tdb, MSG_SMB_SAM_SYNC, NULL, 0, False, NULL);
97 /*************************************************************************
98 net_reply_logon_ctrl2:
99 *************************************************************************/
101 NTSTATUS _net_logon_ctrl2(pipes_struct *p, NET_Q_LOGON_CTRL2 *q_u, NET_R_LOGON_CTRL2 *r_u)
104 uint32 pdc_connection_status = 0x0;
105 uint32 logon_attempts = 0x0;
106 uint32 tc_status = ERROR_NO_LOGON_SERVERS;
107 char *trusted_domain = "test_domain";
109 DEBUG(0, ("*** net long ctrl2 %d, %d, %d\n",
110 q_u->function_code, q_u->query_level, q_u->switch_value));
112 DEBUG(6,("_net_logon_ctrl2: %d\n", __LINE__));
115 /* set up the Logon Control2 response */
116 init_net_r_logon_ctrl2(r_u, q_u->query_level,
117 flags, pdc_connection_status, logon_attempts,
118 tc_status, trusted_domain);
120 if (lp_server_role() == ROLE_DOMAIN_BDC)
123 DEBUG(6,("_net_logon_ctrl2: %d\n", __LINE__));
128 /*************************************************************************
129 net_reply_trust_dom_list:
130 *************************************************************************/
132 NTSTATUS _net_trust_dom_list(pipes_struct *p, NET_Q_TRUST_DOM_LIST *q_u, NET_R_TRUST_DOM_LIST *r_u)
134 char *trusted_domain = "test_domain";
135 uint32 num_trust_domains = 1;
137 DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
139 /* set up the Trusted Domain List response */
140 init_r_trust_dom(r_u, num_trust_domains, trusted_domain);
142 DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
147 /***********************************************************************************
148 init_net_r_srv_pwset:
149 ***********************************************************************************/
151 static void init_net_r_srv_pwset(NET_R_SRV_PWSET *r_s,
152 DOM_CRED *srv_cred, NTSTATUS status)
154 DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
156 memcpy(&r_s->srv_cred, srv_cred, sizeof(r_s->srv_cred));
157 r_s->status = status;
159 DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
162 /******************************************************************
163 gets a machine password entry. checks access rights of the host.
164 ******************************************************************/
166 static BOOL get_md4pw(char *md4pw, char *mach_acct)
168 SAM_ACCOUNT *sampass = NULL;
175 * Currently this code is redundent as we already have a filter
176 * by hostname list. What this code really needs to do is to
177 * get a hosts allowed/hosts denied list from the SAM database
178 * on a per user basis, and make the access decision there.
179 * I will leave this code here for now as a reminder to implement
180 * this at a later date. JRA.
183 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
184 client_name(), client_addr()))
186 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
191 if(!NT_STATUS_IS_OK(pdb_init_sam(&sampass)))
194 /* JRA. This is ok as it is only used for generating the challenge. */
196 ret=pdb_getsampwnam(sampass, mach_acct);
200 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
201 pdb_free_sam(&sampass);
205 acct_ctrl = pdb_get_acct_ctrl(sampass);
206 if (!(acct_ctrl & ACB_DISABLED) &&
207 ((acct_ctrl & ACB_DOMTRUST) ||
208 (acct_ctrl & ACB_WSTRUST) ||
209 (acct_ctrl & ACB_SVRTRUST)) &&
210 ((pass=pdb_get_nt_passwd(sampass)) != NULL)) {
211 memcpy(md4pw, pass, 16);
212 dump_data(5, md4pw, 16);
213 pdb_free_sam(&sampass);
217 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
218 pdb_free_sam(&sampass);
223 /*************************************************************************
225 *************************************************************************/
227 NTSTATUS _net_req_chal(pipes_struct *p, NET_Q_REQ_CHAL *q_u, NET_R_REQ_CHAL *r_u)
229 NTSTATUS status = NT_STATUS_OK;
231 rpcstr_pull(p->dc.remote_machine,q_u->uni_logon_clnt.buffer,sizeof(fstring),q_u->uni_logon_clnt.uni_str_len*2,0);
233 /* create a server challenge for the client */
234 /* Set these to random values. */
235 generate_random_buffer(p->dc.srv_chal.data, 8, False);
237 memcpy(p->dc.srv_cred.challenge.data, p->dc.srv_chal.data, 8);
239 memcpy(p->dc.clnt_chal.data , q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
240 memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
242 memset((char *)p->dc.sess_key, '\0', sizeof(p->dc.sess_key));
244 p->dc.challenge_sent = True;
245 /* set up the LSA REQUEST CHALLENGE response */
246 init_net_r_req_chal(r_u, &p->dc.srv_chal, status);
251 /*************************************************************************
253 *************************************************************************/
255 static void init_net_r_auth(NET_R_AUTH *r_a, DOM_CHAL *resp_cred, NTSTATUS status)
257 memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
258 r_a->status = status;
261 /*************************************************************************
263 *************************************************************************/
265 NTSTATUS _net_auth(pipes_struct *p, NET_Q_AUTH *q_u, NET_R_AUTH *r_u)
267 NTSTATUS status = NT_STATUS_OK;
274 rpcstr_pull(mach_acct, q_u->clnt_id.uni_acct_name.buffer,sizeof(fstring),q_u->clnt_id.uni_acct_name.uni_str_len*2,0);
276 if (p->dc.challenge_sent && get_md4pw((char *)p->dc.md4pw, mach_acct)) {
278 /* from client / server challenges and md4 password, generate sess key */
279 cred_session_key(&p->dc.clnt_chal, &p->dc.srv_chal,
280 (char *)p->dc.md4pw, p->dc.sess_key);
282 /* check that the client credentials are valid */
283 if (cred_assert(&q_u->clnt_chal, p->dc.sess_key, &p->dc.clnt_cred.challenge, srv_time)) {
285 /* create server challenge for inclusion in the reply */
286 cred_create(p->dc.sess_key, &p->dc.srv_cred.challenge, srv_time, &srv_cred);
288 /* copy the received client credentials for use next time */
289 memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
290 memcpy(p->dc.srv_cred .challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
292 /* Save the machine account name. */
293 fstrcpy(p->dc.mach_acct, mach_acct);
295 p->dc.authenticated = True;
298 status = NT_STATUS_ACCESS_DENIED;
301 status = NT_STATUS_ACCESS_DENIED;
304 /* set up the LSA AUTH response */
305 init_net_r_auth(r_u, &srv_cred, status);
310 /*************************************************************************
312 *************************************************************************/
314 static void init_net_r_auth_2(NET_R_AUTH_2 *r_a,
315 DOM_CHAL *resp_cred, NEG_FLAGS *flgs, NTSTATUS status)
317 memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
318 memcpy(&r_a->srv_flgs, flgs, sizeof(r_a->srv_flgs));
319 r_a->status = status;
322 /*************************************************************************
324 *************************************************************************/
326 NTSTATUS _net_auth_2(pipes_struct *p, NET_Q_AUTH_2 *q_u, NET_R_AUTH_2 *r_u)
328 NTSTATUS status = NT_STATUS_OK;
336 rpcstr_pull(mach_acct, q_u->clnt_id.uni_acct_name.buffer,sizeof(fstring),q_u->clnt_id.uni_acct_name.uni_str_len*2,0);
338 if (p->dc.challenge_sent && get_md4pw((char *)p->dc.md4pw, mach_acct)) {
340 /* from client / server challenges and md4 password, generate sess key */
341 cred_session_key(&p->dc.clnt_chal, &p->dc.srv_chal,
342 (char *)p->dc.md4pw, p->dc.sess_key);
344 /* check that the client credentials are valid */
345 if (cred_assert(&q_u->clnt_chal, p->dc.sess_key, &p->dc.clnt_cred.challenge, srv_time)) {
347 /* create server challenge for inclusion in the reply */
348 cred_create(p->dc.sess_key, &p->dc.srv_cred.challenge, srv_time, &srv_cred);
350 /* copy the received client credentials for use next time */
351 memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
352 memcpy(p->dc.srv_cred .challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
354 /* Save the machine account name. */
355 fstrcpy(p->dc.mach_acct, mach_acct);
357 p->dc.authenticated = True;
360 status = NT_STATUS_ACCESS_DENIED;
363 status = NT_STATUS_ACCESS_DENIED;
366 srv_flgs.neg_flags = 0x000001ff;
368 /* set up the LSA AUTH 2 response */
369 init_net_r_auth_2(r_u, &srv_cred, &srv_flgs, status);
374 /*************************************************************************
376 *************************************************************************/
378 NTSTATUS _net_srv_pwset(pipes_struct *p, NET_Q_SRV_PWSET *q_u, NET_R_SRV_PWSET *r_u)
380 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
383 SAM_ACCOUNT *sampass=NULL;
385 unsigned char pwd[16];
389 /* checks and updates credentials. creates reply credentials */
390 if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->clnt_id.cred, &srv_cred)))
391 return NT_STATUS_INVALID_HANDLE;
393 memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
395 DEBUG(5,("_net_srv_pwset: %d\n", __LINE__));
397 rpcstr_pull(workstation,q_u->clnt_id.login.uni_comp_name.buffer,
398 sizeof(workstation),q_u->clnt_id.login.uni_comp_name.uni_str_len*2,0);
400 DEBUG(3,("Server Password Set by Wksta:[%s] on account [%s]\n", workstation, p->dc.mach_acct));
402 pdb_init_sam(&sampass);
405 ret=pdb_getsampwnam(sampass, p->dc.mach_acct);
408 /* Ensure the account exists and is a machine account. */
410 acct_ctrl = pdb_get_acct_ctrl(sampass);
413 && (acct_ctrl & ACB_WSTRUST ||
414 acct_ctrl & ACB_SVRTRUST ||
415 acct_ctrl & ACB_DOMTRUST))) {
416 pdb_free_sam(&sampass);
417 return NT_STATUS_NO_SUCH_USER;
420 if (pdb_get_acct_ctrl(sampass) & ACB_DISABLED) {
421 pdb_free_sam(&sampass);
422 return NT_STATUS_ACCOUNT_DISABLED;
425 DEBUG(100,("Server password set : new given value was :\n"));
426 for(i = 0; i < 16; i++)
427 DEBUG(100,("%02X ", q_u->pwd[i]));
430 cred_hash3( pwd, q_u->pwd, p->dc.sess_key, 0);
432 /* lies! nt and lm passwords are _not_ the same: don't care */
433 if (!pdb_set_lanman_passwd (sampass, pwd)) {
434 pdb_free_sam(&sampass);
435 return NT_STATUS_NO_MEMORY;
438 if (!pdb_set_nt_passwd (sampass, pwd)) {
439 pdb_free_sam(&sampass);
440 return NT_STATUS_NO_MEMORY;
443 if (!pdb_set_pass_changed_now (sampass)) {
444 pdb_free_sam(&sampass);
445 /* Not quite sure what this one qualifies as, but this will do */
446 return NT_STATUS_UNSUCCESSFUL;
450 ret = pdb_update_sam_account (sampass);
454 status = NT_STATUS_OK;
456 /* set up the LSA Server Password Set response */
457 init_net_r_srv_pwset(r_u, &srv_cred, status);
459 pdb_free_sam(&sampass);
464 /*************************************************************************
466 *************************************************************************/
468 NTSTATUS _net_sam_logoff(pipes_struct *p, NET_Q_SAM_LOGOFF *q_u, NET_R_SAM_LOGOFF *r_u)
472 if (!get_valid_user_struct(p->vuid))
473 return NT_STATUS_NO_SUCH_USER;
475 /* checks and updates credentials. creates reply credentials */
476 if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred,
477 &q_u->sam_id.client.cred, &srv_cred)))
478 return NT_STATUS_INVALID_HANDLE;
480 memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
482 /* XXXX maybe we want to say 'no', reject the client's credentials */
483 r_u->buffer_creds = 1; /* yes, we have valid server credentials */
484 memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
486 r_u->status = NT_STATUS_OK;
492 /*************************************************************************
494 *************************************************************************/
496 NTSTATUS _net_sam_logon(pipes_struct *p, NET_Q_SAM_LOGON *q_u, NET_R_SAM_LOGON *r_u)
498 NTSTATUS status = NT_STATUS_OK;
499 NET_USER_INFO_3 *usr_info = NULL;
500 NET_ID_INFO_CTR *ctr = q_u->sam_id.ctr;
502 UNISTR2 *uni_samlogon_user = NULL;
503 UNISTR2 *uni_samlogon_domain = NULL;
504 UNISTR2 *uni_samlogon_workstation = NULL;
505 fstring nt_username, nt_domain, nt_workstation;
506 auth_usersupplied_info *user_info = NULL;
507 auth_serversupplied_info *server_info = NULL;
508 extern userdom_struct current_user_info;
511 usr_info = (NET_USER_INFO_3 *)talloc(p->mem_ctx, sizeof(NET_USER_INFO_3));
513 return NT_STATUS_NO_MEMORY;
515 ZERO_STRUCTP(usr_info);
517 /* store the user information, if there is any. */
518 r_u->user = usr_info;
519 r_u->switch_value = 0; /* indicates no info */
520 r_u->auth_resp = 1; /* authoritative response */
521 r_u->switch_value = 3; /* indicates type of validation user info */
523 if (!get_valid_user_struct(p->vuid))
524 return NT_STATUS_NO_SUCH_USER;
526 /* checks and updates credentials. creates reply credentials */
527 if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->sam_id.client.cred, &srv_cred)))
528 return NT_STATUS_INVALID_HANDLE;
530 memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
532 r_u->buffer_creds = 1; /* yes, we have valid server credentials */
533 memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
535 /* find the username */
537 switch (q_u->sam_id.logon_level) {
538 case INTERACTIVE_LOGON_TYPE:
539 uni_samlogon_user = &ctr->auth.id1.uni_user_name;
540 uni_samlogon_domain = &ctr->auth.id1.uni_domain_name;
542 uni_samlogon_workstation = &ctr->auth.id1.uni_wksta_name;
544 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
547 uni_samlogon_user = &ctr->auth.id2.uni_user_name;
548 uni_samlogon_domain = &ctr->auth.id2.uni_domain_name;
549 uni_samlogon_workstation = &ctr->auth.id2.uni_wksta_name;
551 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
554 DEBUG(2,("SAM Logon: unsupported switch value\n"));
555 return NT_STATUS_INVALID_INFO_CLASS;
558 /* check username exists */
560 rpcstr_pull(nt_username,uni_samlogon_user->buffer,sizeof(nt_username),uni_samlogon_user->uni_str_len*2,0);
561 rpcstr_pull(nt_domain,uni_samlogon_domain->buffer,sizeof(nt_domain),uni_samlogon_domain->uni_str_len*2,0);
562 rpcstr_pull(nt_workstation,uni_samlogon_workstation->buffer,sizeof(nt_workstation),uni_samlogon_workstation->uni_str_len*2,0);
564 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username,
565 nt_workstation, nt_domain));
567 pstrcpy(current_user_info.smb_name, nt_username);
570 * Convert to a UNIX username.
573 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n", q_u->sam_id.ctr->switch_value, nt_username));
575 switch (ctr->switch_value) {
578 struct auth_context *auth_context = NULL;
579 if (!NT_STATUS_IS_OK(status = make_auth_context_fixed(&auth_context, ctr->auth.id2.lm_chal))) {
583 /* Standard challenge/response authenticaion */
584 if (!make_user_info_netlogon_network(&user_info,
585 nt_username, nt_domain,
587 ctr->auth.id2.lm_chal_resp.buffer,
588 ctr->auth.id2.lm_chal_resp.str_str_len,
589 ctr->auth.id2.nt_chal_resp.buffer,
590 ctr->auth.id2.nt_chal_resp.str_str_len)) {
591 status = NT_STATUS_NO_MEMORY;
593 status = auth_context->check_ntlm_password(auth_context, user_info, &server_info);
595 (auth_context->free)(&auth_context);
599 case INTERACTIVE_LOGON_TYPE:
600 /* 'Interactive' autheticaion, supplies the password in its
601 MD4 form, encrypted with the session key. We will
602 convert this to chellange/responce for the auth
603 subsystem to chew on */
605 struct auth_context *auth_context = NULL;
607 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
611 chal = auth_context->get_ntlm_challenge(auth_context);
613 if (!make_user_info_netlogon_interactive(&user_info,
614 nt_username, nt_domain,
615 nt_workstation, chal,
616 ctr->auth.id1.lm_owf.data,
617 ctr->auth.id1.nt_owf.data,
619 status = NT_STATUS_NO_MEMORY;
621 status = auth_context->check_ntlm_password(auth_context, user_info, &server_info);
624 (auth_context->free)(&auth_context);
629 DEBUG(2,("SAM Logon: unsupported switch value\n"));
630 return NT_STATUS_INVALID_INFO_CLASS;
633 free_user_info(&user_info);
635 DEBUG(5, ("_net_sam_logon: check_password returned status %s\n",
638 /* Check account and password */
640 if (!NT_STATUS_IS_OK(status)) {
641 free_server_info(&server_info);
645 if (server_info->guest) {
646 /* We don't like guest domain logons... */
647 DEBUG(5,("_net_sam_logon: Attempted domain logon as GUEST denied.\n"));
648 free_server_info(&server_info);
649 return NT_STATUS_LOGON_FAILURE;
652 /* This is the point at which, if the login was successful, that
653 the SAM Local Security Authority should record that the user is
654 logged in to the domain. */
657 DOM_GID *gids = NULL;
660 pstring my_workgroup;
662 /* set up pointer indicating user/password failed to be found */
663 usr_info->ptr_user_info = 0;
665 pstrcpy(my_workgroup, lp_workgroup());
666 pstrcpy(my_name, global_myname);
670 * This is the point at which we get the group
671 * database - we should be getting the gid_t list
672 * from /etc/group and then turning the uids into
673 * rids and then into machine sids for this user.
678 get_domain_user_groups(p->mem_ctx, &num_gids, &gids, server_info->sam_account);
680 sampw = server_info->sam_account;
682 init_net_user_info3(p->mem_ctx, usr_info,
683 pdb_get_user_rid(sampw),
684 pdb_get_group_rid(sampw),
686 pdb_get_username(sampw),
687 pdb_get_fullname(sampw),
688 pdb_get_homedir(sampw),
689 pdb_get_dirdrive(sampw),
690 pdb_get_logon_script(sampw),
691 pdb_get_profile_path(sampw),
692 pdb_get_logon_time(sampw),
693 pdb_get_logoff_time(sampw),
694 pdb_get_kickoff_time(sampw),
695 pdb_get_pass_last_set_time(sampw),
696 pdb_get_pass_can_change_time(sampw),
697 pdb_get_pass_must_change_time(sampw),
700 0, /* bad_pw_count */
701 num_gids, /* uint32 num_groups */
702 gids , /* DOM_GID *gids */
703 0x20 , /* uint32 user_flgs (?) */
704 NULL, /* uchar sess_key[16] */
705 my_name , /* char *logon_srv */
706 my_workgroup, /* char *logon_dom */
707 get_global_sam_sid(), /* DOM_SID *dom_sid */
708 /* Should be users domain sid, not servers - for trusted domains */
710 NULL); /* char *other_sids */
712 free_server_info(&server_info);