2 * Unix SMB/Netbios implementation.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-1997,
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
7 * Copyright (C) Paul Ashton 1997.
8 * Copyright (C) Jeremy Allison 1998-2001.
9 * Copyirht (C) Andrew Bartlett 2001.
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 2 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, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* This is the implementation of the netlogon pipe. */
30 extern pstring global_myname;
31 extern DOM_SID global_sam_sid;
33 /*************************************************************************
35 *************************************************************************/
37 static void init_net_r_req_chal(NET_R_REQ_CHAL *r_c,
38 DOM_CHAL *srv_chal, NTSTATUS status)
40 DEBUG(6,("init_net_r_req_chal: %d\n", __LINE__));
41 memcpy(r_c->srv_chal.data, srv_chal->data, sizeof(srv_chal->data));
45 /*************************************************************************
46 error messages cropping up when using nltest.exe...
47 *************************************************************************/
49 #define ERROR_NO_SUCH_DOMAIN 0x54b
50 #define ERROR_NO_LOGON_SERVERS 0x51f
52 /*************************************************************************
54 *************************************************************************/
56 /* Some flag values reverse engineered from NLTEST.EXE */
58 #define LOGON_CTRL_IN_SYNC 0x00
59 #define LOGON_CTRL_REPL_NEEDED 0x01
60 #define LOGON_CTRL_REPL_IN_PROGRESS 0x02
62 NTSTATUS _net_logon_ctrl(pipes_struct *p, NET_Q_LOGON_CTRL *q_u,
63 NET_R_LOGON_CTRL *r_u)
66 uint32 pdc_connection_status = 0x00; /* Maybe a win32 error code? */
68 /* Setup the Logon Control response */
70 init_net_r_logon_ctrl(r_u, q_u->query_level, flags,
71 pdc_connection_status);
76 /****************************************************************************
77 Send a message to smbd to do a sam synchronisation
78 **************************************************************************/
79 static void send_sync_message(void)
83 tdb = tdb_open_log(lock_path("connections.tdb"), 0,
84 TDB_DEFAULT, O_RDONLY, 0);
87 DEBUG(3, ("send_sync_message(): failed to open connections "
92 DEBUG(3, ("sending sam synchronisation message\n"));
94 message_send_all(tdb, MSG_SMB_SAM_SYNC, NULL, 0, False, NULL);
99 /*************************************************************************
100 net_reply_logon_ctrl2:
101 *************************************************************************/
103 NTSTATUS _net_logon_ctrl2(pipes_struct *p, NET_Q_LOGON_CTRL2 *q_u, NET_R_LOGON_CTRL2 *r_u)
106 uint32 pdc_connection_status = 0x0;
107 uint32 logon_attempts = 0x0;
108 uint32 tc_status = ERROR_NO_LOGON_SERVERS;
109 char *trusted_domain = "test_domain";
111 DEBUG(0, ("*** net long ctrl2 %d, %d, %d\n",
112 q_u->function_code, q_u->query_level, q_u->switch_value));
114 DEBUG(6,("_net_logon_ctrl2: %d\n", __LINE__));
117 /* set up the Logon Control2 response */
118 init_net_r_logon_ctrl2(r_u, q_u->query_level,
119 flags, pdc_connection_status, logon_attempts,
120 tc_status, trusted_domain);
122 if (lp_server_role() == ROLE_DOMAIN_BDC)
125 DEBUG(6,("_net_logon_ctrl2: %d\n", __LINE__));
130 /*************************************************************************
131 net_reply_trust_dom_list:
132 *************************************************************************/
134 NTSTATUS _net_trust_dom_list(pipes_struct *p, NET_Q_TRUST_DOM_LIST *q_u, NET_R_TRUST_DOM_LIST *r_u)
136 char *trusted_domain = "test_domain";
137 uint32 num_trust_domains = 1;
139 DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
141 /* set up the Trusted Domain List response */
142 init_r_trust_dom(r_u, num_trust_domains, trusted_domain);
144 DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
149 /***********************************************************************************
150 init_net_r_srv_pwset:
151 ***********************************************************************************/
153 static void init_net_r_srv_pwset(NET_R_SRV_PWSET *r_s,
154 DOM_CRED *srv_cred, NTSTATUS status)
156 DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
158 memcpy(&r_s->srv_cred, srv_cred, sizeof(r_s->srv_cred));
159 r_s->status = status;
161 DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
164 /******************************************************************
165 gets a machine password entry. checks access rights of the host.
166 ******************************************************************/
168 static BOOL get_md4pw(char *md4pw, char *mach_acct)
170 SAM_ACCOUNT *sampass = NULL;
176 * Currently this code is redundent as we already have a filter
177 * by hostname list. What this code really needs to do is to
178 * get a hosts allowed/hosts denied list from the SAM database
179 * on a per user basis, and make the access decision there.
180 * I will leave this code here for now as a reminder to implement
181 * this at a later date. JRA.
184 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
185 client_name(), client_addr()))
187 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
192 if(!pdb_init_sam(&sampass))
195 /* JRA. This is ok as it is only used for generating the challenge. */
197 ret=pdb_getsampwnam(sampass, mach_acct);
201 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
202 pdb_free_sam(&sampass);
206 if (!(pdb_get_acct_ctrl(sampass) & ACB_DISABLED) && ((pass=pdb_get_nt_passwd(sampass)) != NULL)) {
207 memcpy(md4pw, pass, 16);
208 dump_data(5, md4pw, 16);
209 pdb_free_sam(&sampass);
213 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
214 pdb_free_sam(&sampass);
219 /*************************************************************************
221 *************************************************************************/
223 NTSTATUS _net_req_chal(pipes_struct *p, NET_Q_REQ_CHAL *q_u, NET_R_REQ_CHAL *r_u)
225 NTSTATUS status = NT_STATUS_OK;
228 if (!get_valid_user_struct(p->vuid))
229 return NT_STATUS_NO_SUCH_USER;
231 rpcstr_pull(mach_acct,q_u->uni_logon_clnt.buffer,sizeof(fstring),q_u->uni_logon_clnt.uni_str_len*2,0);
234 fstrcat(mach_acct, "$");
236 if (get_md4pw((char *)p->dc.md4pw, mach_acct)) {
237 /* copy the client credentials */
238 memcpy(p->dc.clnt_chal.data , q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
239 memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
241 /* create a server challenge for the client */
242 /* Set these to random values. */
243 generate_random_buffer(p->dc.srv_chal.data, 8, False);
245 memcpy(p->dc.srv_cred.challenge.data, p->dc.srv_chal.data, 8);
247 memset((char *)p->dc.sess_key, '\0', sizeof(p->dc.sess_key));
249 /* from client / server challenges and md4 password, generate sess key */
250 cred_session_key(&p->dc.clnt_chal, &p->dc.srv_chal,
251 (char *)p->dc.md4pw, p->dc.sess_key);
253 /* Save the machine account name. */
254 fstrcpy(p->dc.mach_acct, mach_acct);
257 /* lkclXXXX take a guess at a good error message to return :-) */
258 status = NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
261 /* set up the LSA REQUEST CHALLENGE response */
262 init_net_r_req_chal(r_u, &p->dc.srv_chal, status);
267 /*************************************************************************
269 *************************************************************************/
271 static void init_net_r_auth(NET_R_AUTH *r_a, DOM_CHAL *resp_cred, NTSTATUS status)
273 memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
274 r_a->status = status;
277 /*************************************************************************
279 *************************************************************************/
281 NTSTATUS _net_auth(pipes_struct *p, NET_Q_AUTH *q_u, NET_R_AUTH *r_u)
283 NTSTATUS status = NT_STATUS_OK;
287 if (!get_valid_user_struct(p->vuid))
288 return NT_STATUS_NO_SUCH_USER;
292 /* check that the client credentials are valid */
293 if (cred_assert(&q_u->clnt_chal, p->dc.sess_key, &p->dc.clnt_cred.challenge, srv_time)) {
295 /* create server challenge for inclusion in the reply */
296 cred_create(p->dc.sess_key, &p->dc.srv_cred.challenge, srv_time, &srv_cred);
298 /* copy the received client credentials for use next time */
299 memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
300 memcpy(p->dc.srv_cred .challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
302 status = NT_STATUS_ACCESS_DENIED;
305 /* set up the LSA AUTH 2 response */
306 init_net_r_auth(r_u, &srv_cred, status);
311 /*************************************************************************
313 *************************************************************************/
315 static void init_net_r_auth_2(NET_R_AUTH_2 *r_a,
316 DOM_CHAL *resp_cred, NEG_FLAGS *flgs, NTSTATUS status)
318 memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
319 memcpy(&r_a->srv_flgs, flgs, sizeof(r_a->srv_flgs));
320 r_a->status = status;
323 /*************************************************************************
325 *************************************************************************/
327 NTSTATUS _net_auth_2(pipes_struct *p, NET_Q_AUTH_2 *q_u, NET_R_AUTH_2 *r_u)
329 NTSTATUS status = NT_STATUS_OK;
334 if (!get_valid_user_struct(p->vuid))
335 return NT_STATUS_NO_SUCH_USER;
339 /* check that the client credentials are valid */
340 if (cred_assert(&q_u->clnt_chal, p->dc.sess_key, &p->dc.clnt_cred.challenge, srv_time)) {
342 /* create server challenge for inclusion in the reply */
343 cred_create(p->dc.sess_key, &p->dc.srv_cred.challenge, srv_time, &srv_cred);
345 /* copy the received client credentials for use next time */
346 memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
347 memcpy(p->dc.srv_cred .challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
349 status = NT_STATUS_ACCESS_DENIED;
352 srv_flgs.neg_flags = 0x000001ff;
354 /* set up the LSA AUTH 2 response */
355 init_net_r_auth_2(r_u, &srv_cred, &srv_flgs, status);
360 /*************************************************************************
362 *************************************************************************/
364 NTSTATUS _net_srv_pwset(pipes_struct *p, NET_Q_SRV_PWSET *q_u, NET_R_SRV_PWSET *r_u)
366 NTSTATUS status = NT_STATUS_WRONG_PASSWORD;
369 SAM_ACCOUNT *sampass=NULL;
371 unsigned char pwd[16];
374 if (!get_valid_user_struct(p->vuid))
375 return NT_STATUS_NO_SUCH_USER;
377 /* checks and updates credentials. creates reply credentials */
378 if (!deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->clnt_id.cred, &srv_cred))
379 return NT_STATUS_INVALID_HANDLE;
381 memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
383 DEBUG(5,("_net_srv_pwset: %d\n", __LINE__));
385 rpcstr_pull(mach_acct,q_u->clnt_id.login.uni_acct_name.buffer,
386 sizeof(mach_acct),q_u->clnt_id.login.uni_acct_name.uni_str_len*2,0);
388 DEBUG(3,("Server Password Set Wksta:[%s]\n", mach_acct));
391 * Check the machine account name we're changing is the same
392 * as the one we've authenticated from. This prevents arbitrary
393 * machines changing other machine account passwords.
396 if (!strequal(mach_acct, p->dc.mach_acct)) {
397 return NT_STATUS_ACCESS_DENIED;
400 pdb_init_sam(&sampass);
403 ret=pdb_getsampwnam(sampass, mach_acct);
406 /* Ensure the account exists and is a machine account. */
408 if (ret==False || !(pdb_get_acct_ctrl(sampass) & ACB_WSTRUST)) {
409 pdb_free_sam(&sampass);
410 return NT_STATUS_NO_SUCH_USER;
413 DEBUG(100,("Server password set : new given value was :\n"));
414 for(i = 0; i < 16; i++)
415 DEBUG(100,("%02X ", q_u->pwd[i]));
418 cred_hash3( pwd, q_u->pwd, p->dc.sess_key, 0);
420 /* lies! nt and lm passwords are _not_ the same: don't care */
421 if (!pdb_set_lanman_passwd (sampass, pwd)) {
422 pdb_free_sam(&sampass);
423 return NT_STATUS_NO_MEMORY;
426 if (!pdb_set_nt_passwd (sampass, pwd)) {
427 pdb_free_sam(&sampass);
428 return NT_STATUS_NO_MEMORY;
431 if (!pdb_set_acct_ctrl (sampass, ACB_WSTRUST)) {
432 pdb_free_sam(&sampass);
433 /* Not quite sure what this one qualifies as, but this will do */
434 return NT_STATUS_NO_MEMORY;
437 if (!pdb_set_pass_changed_now (sampass)) {
438 pdb_free_sam(&sampass);
439 /* Not quite sure what this one qualifies as, but this will do */
440 return NT_STATUS_NO_MEMORY;
444 ret = pdb_update_sam_account (sampass,False);
448 status = NT_STATUS_OK;
450 /* set up the LSA Server Password Set response */
451 init_net_r_srv_pwset(r_u, &srv_cred, status);
453 pdb_free_sam(&sampass);
458 /*************************************************************************
460 *************************************************************************/
462 NTSTATUS _net_sam_logoff(pipes_struct *p, NET_Q_SAM_LOGOFF *q_u, NET_R_SAM_LOGOFF *r_u)
466 if (!get_valid_user_struct(p->vuid))
467 return NT_STATUS_NO_SUCH_USER;
469 /* checks and updates credentials. creates reply credentials */
470 if (!deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred,
471 &q_u->sam_id.client.cred, &srv_cred))
472 return NT_STATUS_INVALID_HANDLE;
474 memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
476 /* XXXX maybe we want to say 'no', reject the client's credentials */
477 r_u->buffer_creds = 1; /* yes, we have valid server credentials */
478 memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
480 r_u->status = NT_STATUS_OK;
486 /*************************************************************************
488 *************************************************************************/
490 NTSTATUS _net_sam_logon(pipes_struct *p, NET_Q_SAM_LOGON *q_u, NET_R_SAM_LOGON *r_u)
492 NTSTATUS status = NT_STATUS_OK;
493 NET_USER_INFO_3 *usr_info = NULL;
494 NET_ID_INFO_CTR *ctr = q_u->sam_id.ctr;
496 UNISTR2 *uni_samlogon_user = NULL;
497 UNISTR2 *uni_samlogon_domain = NULL;
498 UNISTR2 *uni_samlogon_workstation = NULL;
499 fstring nt_username, nt_domain, nt_workstation;
500 auth_usersupplied_info *user_info = NULL;
501 auth_serversupplied_info *server_info = NULL;
502 extern userdom_struct current_user_info;
504 usr_info = (NET_USER_INFO_3 *)talloc(p->mem_ctx, sizeof(NET_USER_INFO_3));
506 return NT_STATUS_NO_MEMORY;
508 ZERO_STRUCTP(usr_info);
510 if (!get_valid_user_struct(p->vuid))
511 return NT_STATUS_NO_SUCH_USER;
513 /* checks and updates credentials. creates reply credentials */
514 if (!deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->sam_id.client.cred, &srv_cred))
515 return NT_STATUS_INVALID_HANDLE;
517 memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
519 r_u->buffer_creds = 1; /* yes, we have valid server credentials */
520 memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
522 /* store the user information, if there is any. */
523 r_u->user = usr_info;
524 r_u->switch_value = 0; /* indicates no info */
525 r_u->auth_resp = 1; /* authoritative response */
526 r_u->switch_value = 3; /* indicates type of validation user info */
528 /* find the username */
530 switch (q_u->sam_id.logon_level) {
531 case INTERACTIVE_LOGON_TYPE:
532 uni_samlogon_user = &ctr->auth.id1.uni_user_name;
533 uni_samlogon_domain = &ctr->auth.id1.uni_domain_name;
535 uni_samlogon_workstation = &ctr->auth.id1.uni_wksta_name;
537 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
540 uni_samlogon_user = &ctr->auth.id2.uni_user_name;
541 uni_samlogon_domain = &ctr->auth.id2.uni_domain_name;
542 uni_samlogon_workstation = &ctr->auth.id2.uni_wksta_name;
544 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
547 DEBUG(2,("SAM Logon: unsupported switch value\n"));
548 return NT_STATUS_INVALID_INFO_CLASS;
551 /* check username exists */
553 rpcstr_pull(nt_username,uni_samlogon_user->buffer,sizeof(nt_username),uni_samlogon_user->uni_str_len*2,0);
554 rpcstr_pull(nt_domain,uni_samlogon_domain->buffer,sizeof(nt_domain),uni_samlogon_domain->uni_str_len*2,0);
555 rpcstr_pull(nt_workstation,uni_samlogon_workstation->buffer,sizeof(nt_workstation),uni_samlogon_workstation->uni_str_len*2,0);
557 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username,
558 nt_workstation, nt_domain));
560 pstrcpy(current_user_info.smb_name, nt_username);
563 * Convert to a UNIX username.
566 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n", q_u->sam_id.ctr->switch_value, nt_username));
568 switch (ctr->switch_value) {
571 struct auth_context *auth_context = NULL;
572 if (!NT_STATUS_IS_OK(status = make_auth_context_fixed(&auth_context, ctr->auth.id2.lm_chal))) {
576 /* Standard challenge/response authenticaion */
577 if (!make_user_info_netlogon_network(&user_info,
578 nt_username, nt_domain,
580 ctr->auth.id2.lm_chal_resp.buffer,
581 ctr->auth.id2.lm_chal_resp.str_str_len,
582 ctr->auth.id2.nt_chal_resp.buffer,
583 ctr->auth.id2.nt_chal_resp.str_str_len)) {
584 status = NT_STATUS_NO_MEMORY;
586 status = auth_context->check_ntlm_password(auth_context, user_info, &server_info);
588 auth_context->free(&auth_context);
592 case INTERACTIVE_LOGON_TYPE:
593 /* 'Interactive' autheticaion, supplies the password in its
594 MD4 form, encrypted with the session key. We will
595 convert this to chellange/responce for the auth
596 subsystem to chew on */
598 struct auth_context *auth_context = NULL;
600 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
604 chal = auth_context->get_ntlm_challenge(auth_context);
606 if (!make_user_info_netlogon_interactive(&user_info,
607 nt_username, nt_domain,
608 nt_workstation, chal,
609 ctr->auth.id1.lm_owf.data,
610 ctr->auth.id1.nt_owf.data,
612 status = NT_STATUS_NO_MEMORY;
614 status = auth_context->check_ntlm_password(auth_context, user_info, &server_info);
617 auth_context->free(&auth_context);
622 DEBUG(2,("SAM Logon: unsupported switch value\n"));
623 return NT_STATUS_INVALID_INFO_CLASS;
626 free_user_info(&user_info);
628 DEBUG(5, ("_net_sam_logon: check_password returned status %s\n",
629 get_nt_error_msg(status)));
631 /* Check account and password */
633 if (!NT_STATUS_IS_OK(status)) {
634 free_server_info(&server_info);
638 if (server_info->guest) {
639 /* We don't like guest domain logons... */
640 DEBUG(5,("_net_sam_logon: Attempted domain logon as GUEST denied.\n"));
641 free_server_info(&server_info);
642 return NT_STATUS_LOGON_FAILURE;
645 /* This is the point at which, if the login was successful, that
646 the SAM Local Security Authority should record that the user is
647 logged in to the domain. */
650 DOM_GID *gids = NULL;
653 pstring my_workgroup;
655 /* set up pointer indicating user/password failed to be found */
656 usr_info->ptr_user_info = 0;
658 pstrcpy(my_workgroup, lp_workgroup());
659 pstrcpy(my_name, global_myname);
663 * This is the point at which we get the group
664 * database - we should be getting the gid_t list
665 * from /etc/group and then turning the uids into
666 * rids and then into machine sids for this user.
671 get_domain_user_groups(p->mem_ctx, &num_gids, &gids, server_info->sam_account);
673 init_net_user_info3(p->mem_ctx, usr_info, server_info->sam_account,
675 0, /* bad_pw_count */
676 num_gids, /* uint32 num_groups */
677 gids , /* DOM_GID *gids */
678 0x20 , /* uint32 user_flgs (?) */
679 NULL, /* uchar sess_key[16] */
680 my_name , /* char *logon_srv */
681 my_workgroup, /* char *logon_dom */
682 &global_sam_sid, /* DOM_SID *dom_sid */
683 NULL); /* char *other_sids */
686 free_server_info(&server_info);