r13576: This is the beginnings of moving the SAM_ACCOUNT data structure
[ira/wip.git] / source3 / rpc_server / srv_netlog_nt.c
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  *
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.
14  *  
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.
19  *  
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.
23  */
24
25 /* This is the implementation of the netlogon pipe. */
26
27 #include "includes.h"
28
29 extern userdom_struct current_user_info;
30
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_RPC_SRV
33
34 /*************************************************************************
35  init_net_r_req_chal:
36  *************************************************************************/
37
38 static void init_net_r_req_chal(NET_R_REQ_CHAL *r_c,
39                                 DOM_CHAL *srv_chal, NTSTATUS status)
40 {
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));
43         r_c->status = status;
44 }
45
46 /*************************************************************************
47  error messages cropping up when using nltest.exe...
48  *************************************************************************/
49
50 #define ERROR_NO_SUCH_DOMAIN   0x54b
51 #define ERROR_NO_LOGON_SERVERS 0x51f
52 #define NO_ERROR               0x0
53
54 /*************************************************************************
55  net_reply_logon_ctrl:
56  *************************************************************************/
57
58 NTSTATUS _net_logon_ctrl(pipes_struct *p, NET_Q_LOGON_CTRL *q_u, 
59                        NET_R_LOGON_CTRL *r_u)
60 {
61         uint32 flags = 0x0;
62         uint32 pdc_connection_status = 0x00; /* Maybe a win32 error code? */
63         
64         /* Setup the Logon Control response */
65
66         init_net_r_logon_ctrl(r_u, q_u->query_level, flags, 
67                               pdc_connection_status);
68
69         return r_u->status;
70 }
71
72 /****************************************************************************
73 Send a message to smbd to do a sam synchronisation
74 **************************************************************************/
75
76 static void send_sync_message(void)
77 {
78         TDB_CONTEXT *tdb;
79
80         tdb = tdb_open_log(lock_path("connections.tdb"), 0,
81                            TDB_DEFAULT, O_RDONLY, 0);
82
83         if (!tdb) {
84                 DEBUG(3, ("send_sync_message(): failed to open connections "
85                           "database\n"));
86                 return;
87         }
88
89         DEBUG(3, ("sending sam synchronisation message\n"));
90         
91         message_send_all(tdb, MSG_SMB_SAM_SYNC, NULL, 0, False, NULL);
92
93         tdb_close(tdb);
94 }
95
96 /*************************************************************************
97  net_reply_logon_ctrl2:
98  *************************************************************************/
99
100 NTSTATUS _net_logon_ctrl2(pipes_struct *p, NET_Q_LOGON_CTRL2 *q_u, NET_R_LOGON_CTRL2 *r_u)
101 {
102         uint32 flags = 0x0;
103         uint32 pdc_connection_status = 0x0;
104         uint32 logon_attempts = 0x0;
105         uint32 tc_status;
106         fstring servername, domain, dc_name, dc_name2;
107         struct in_addr dc_ip;
108
109         /* this should be \\global_myname() */
110         unistr2_to_ascii(servername, &q_u->uni_server_name, sizeof(servername));
111
112         r_u->status = NT_STATUS_OK;
113         
114         tc_status = ERROR_NO_SUCH_DOMAIN;
115         fstrcpy( dc_name, "" );
116         
117         switch ( q_u->function_code ) {
118                 case NETLOGON_CONTROL_TC_QUERY:
119                         unistr2_to_ascii(domain, &q_u->info.info6.domain, sizeof(domain));
120                                 
121                         if ( !is_trusted_domain( domain ) )
122                                 break;
123                                 
124                         if ( !get_dc_name( domain, NULL, dc_name2, &dc_ip ) ) {
125                                 tc_status = ERROR_NO_LOGON_SERVERS;
126                                 break;
127                         }
128
129                         fstr_sprintf( dc_name, "\\\\%s", dc_name2 );
130                                 
131                         tc_status = NO_ERROR;
132                         
133                         break;
134                         
135                 case NETLOGON_CONTROL_REDISCOVER:
136                         unistr2_to_ascii(domain, &q_u->info.info6.domain, sizeof(domain));
137                                 
138                         if ( !is_trusted_domain( domain ) )
139                                 break;
140                                 
141                         if ( !get_dc_name( domain, NULL, dc_name2, &dc_ip ) ) {
142                                 tc_status = ERROR_NO_LOGON_SERVERS;
143                                 break;
144                         }
145
146                         fstr_sprintf( dc_name, "\\\\%s", dc_name2 );
147                                 
148                         tc_status = NO_ERROR;
149                         
150                         break;
151                         
152                 default:
153                         /* no idea what this should be */
154                         DEBUG(0,("_net_logon_ctrl2: unimplemented function level [%d]\n",
155                                 q_u->function_code));
156         }
157         
158         /* prepare the response */
159         
160         init_net_r_logon_ctrl2( r_u, q_u->query_level, flags, 
161                 pdc_connection_status, logon_attempts, tc_status, dc_name );
162
163         if (lp_server_role() == ROLE_DOMAIN_BDC)
164                 send_sync_message();
165
166         return r_u->status;
167 }
168
169 /*************************************************************************
170  net_reply_trust_dom_list:
171  *************************************************************************/
172
173 NTSTATUS _net_trust_dom_list(pipes_struct *p, NET_Q_TRUST_DOM_LIST *q_u, NET_R_TRUST_DOM_LIST *r_u)
174 {
175         const char *trusted_domain = "test_domain";
176         uint32 num_trust_domains = 1;
177
178         DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
179
180         /* set up the Trusted Domain List response */
181         init_r_trust_dom(r_u, num_trust_domains, trusted_domain);
182
183         DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
184
185         return r_u->status;
186 }
187
188 /***********************************************************************************
189  init_net_r_srv_pwset:
190  ***********************************************************************************/
191
192 static void init_net_r_srv_pwset(NET_R_SRV_PWSET *r_s,
193                                  DOM_CRED *srv_cred, NTSTATUS status)  
194 {
195         DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
196
197         memcpy(&r_s->srv_cred, srv_cred, sizeof(r_s->srv_cred));
198         r_s->status = status;
199
200         DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
201 }
202
203 /******************************************************************
204  gets a machine password entry.  checks access rights of the host.
205  ******************************************************************/
206
207 static BOOL get_md4pw(char *md4pw, char *mach_acct)
208 {
209         struct samu *sampass = NULL;
210         const uint8 *pass;
211         BOOL ret;
212         uint32 acct_ctrl;
213
214 #if 0
215     /*
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.
222      */
223
224         if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
225                           client_name(), client_addr()))
226         {
227                 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
228                 return False;
229         }
230 #endif /* 0 */
231
232         if(!NT_STATUS_IS_OK(pdb_init_sam(&sampass)))
233                 return False;
234
235         /* JRA. This is ok as it is only used for generating the challenge. */
236         become_root();
237         ret=pdb_getsampwnam(sampass, mach_acct);
238         unbecome_root();
239  
240         if (ret==False) {
241                 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
242                 TALLOC_FREE(sampass);
243                 return False;
244         }
245
246         acct_ctrl = pdb_get_acct_ctrl(sampass);
247         if (!(acct_ctrl & ACB_DISABLED) &&
248             ((acct_ctrl & ACB_DOMTRUST) ||
249              (acct_ctrl & ACB_WSTRUST) ||
250              (acct_ctrl & ACB_SVRTRUST)) &&
251             ((pass=pdb_get_nt_passwd(sampass)) != NULL)) {
252                 memcpy(md4pw, pass, 16);
253                 dump_data(5, md4pw, 16);
254                 TALLOC_FREE(sampass);
255                 return True;
256         }
257         
258         DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
259         TALLOC_FREE(sampass);
260         return False;
261
262 }
263
264 /*************************************************************************
265  _net_req_chal
266  *************************************************************************/
267
268 NTSTATUS _net_req_chal(pipes_struct *p, NET_Q_REQ_CHAL *q_u, NET_R_REQ_CHAL *r_u)
269 {
270         if (!p->dc) {
271                 p->dc = TALLOC_ZERO_P(p->pipe_state_mem_ctx, struct dcinfo);
272                 if (!p->dc) {
273                         return NT_STATUS_NO_MEMORY;
274                 }
275         } else {
276                 DEBUG(10,("_net_req_chal: new challenge requested. Clearing old state.\n"));
277                 ZERO_STRUCTP(p->dc);
278         }
279
280         rpcstr_pull(p->dc->remote_machine,
281                         q_u->uni_logon_clnt.buffer,
282                         sizeof(fstring),q_u->uni_logon_clnt.uni_str_len*2,0);
283
284         /* Save the client challenge to the server. */
285         memcpy(p->dc->clnt_chal.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
286
287         /* Create a server challenge for the client */
288         /* Set this to a random value. */
289         generate_random_buffer(p->dc->srv_chal.data, 8);
290         
291         /* set up the LSA REQUEST CHALLENGE response */
292         init_net_r_req_chal(r_u, &p->dc->srv_chal, NT_STATUS_OK);
293         
294         p->dc->challenge_sent = True;
295
296         return NT_STATUS_OK;
297 }
298
299 /*************************************************************************
300  init_net_r_auth:
301  *************************************************************************/
302
303 static void init_net_r_auth(NET_R_AUTH *r_a, DOM_CHAL *resp_cred, NTSTATUS status)
304 {
305         memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
306         r_a->status = status;
307 }
308
309 /*************************************************************************
310  _net_auth. Create the initial credentials.
311  *************************************************************************/
312
313 NTSTATUS _net_auth(pipes_struct *p, NET_Q_AUTH *q_u, NET_R_AUTH *r_u)
314 {
315         fstring mach_acct;
316         fstring remote_machine;
317         DOM_CHAL srv_chal_out;
318
319         if (!p->dc || !p->dc->challenge_sent) {
320                 return NT_STATUS_ACCESS_DENIED;
321         }
322
323         rpcstr_pull(mach_acct, q_u->clnt_id.uni_acct_name.buffer,sizeof(fstring),
324                                 q_u->clnt_id.uni_acct_name.uni_str_len*2,0);
325         rpcstr_pull(remote_machine, q_u->clnt_id.uni_comp_name.buffer,sizeof(fstring),
326                                 q_u->clnt_id.uni_comp_name.uni_str_len*2,0);
327
328         if (!get_md4pw((char *)p->dc->mach_pw, mach_acct)) {
329                 DEBUG(0,("_net_auth: creds_server_check failed. Failed to "
330                         "get pasword for machine account %s "
331                         "from client %s\n",
332                         mach_acct, remote_machine ));
333                 return NT_STATUS_ACCESS_DENIED;
334         }
335
336         /* From the client / server challenges and md4 password, generate sess key */
337         creds_server_init(0,                    /* No neg flags. */
338                         p->dc,
339                         &p->dc->clnt_chal,      /* Stored client chal. */
340                         &p->dc->srv_chal,       /* Stored server chal. */
341                         p->dc->mach_pw,
342                         &srv_chal_out); 
343
344         /* Check client credentials are valid. */
345         if (!creds_server_check(p->dc, &q_u->clnt_chal)) {
346                 DEBUG(0,("_net_auth: creds_server_check failed. Rejecting auth "
347                         "request from client %s machine account %s\n",
348                         remote_machine, mach_acct ));
349                 return NT_STATUS_ACCESS_DENIED;
350         }
351
352         fstrcpy(p->dc->mach_acct, mach_acct);
353         fstrcpy(p->dc->remote_machine, remote_machine);
354         p->dc->authenticated = True;
355
356         /* set up the LSA AUTH response */
357         /* Return the server credentials. */
358         init_net_r_auth(r_u, &srv_chal_out, NT_STATUS_OK);
359
360         return r_u->status;
361 }
362
363 /*************************************************************************
364  init_net_r_auth_2:
365  *************************************************************************/
366
367 static void init_net_r_auth_2(NET_R_AUTH_2 *r_a,
368                               DOM_CHAL *resp_cred, NEG_FLAGS *flgs, NTSTATUS status)
369 {
370         memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
371         memcpy(&r_a->srv_flgs, flgs, sizeof(r_a->srv_flgs));
372         r_a->status = status;
373 }
374
375 /*************************************************************************
376  _net_auth_2
377  *************************************************************************/
378
379 NTSTATUS _net_auth_2(pipes_struct *p, NET_Q_AUTH_2 *q_u, NET_R_AUTH_2 *r_u)
380 {
381         NEG_FLAGS srv_flgs;
382         fstring mach_acct;
383         fstring remote_machine;
384         DOM_CHAL srv_chal_out;
385
386         rpcstr_pull(mach_acct, q_u->clnt_id.uni_acct_name.buffer,sizeof(fstring),
387                                 q_u->clnt_id.uni_acct_name.uni_str_len*2,0);
388
389         /* We use this as the key to store the creds. */
390         rpcstr_pull(remote_machine, q_u->clnt_id.uni_comp_name.buffer,sizeof(fstring),
391                                 q_u->clnt_id.uni_comp_name.uni_str_len*2,0);
392
393         if (!p->dc || !p->dc->challenge_sent) {
394                 DEBUG(0,("_net_auth2: no challenge sent to client %s\n",
395                         remote_machine ));
396                 return NT_STATUS_ACCESS_DENIED;
397         }
398
399         if ( (lp_server_schannel() == True) &&
400              ((q_u->clnt_flgs.neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
401
402                 /* schannel must be used, but client did not offer it. */
403                 DEBUG(0,("_net_auth2: schannel required but client failed "
404                         "to offer it. Client was %s\n",
405                         mach_acct ));
406                 return NT_STATUS_ACCESS_DENIED;
407         }
408
409         if (!get_md4pw((char *)p->dc->mach_pw, mach_acct)) {
410                 DEBUG(0,("_net_auth2: failed to get machine password for "
411                         "account %s\n",
412                         mach_acct ));
413                 return NT_STATUS_ACCESS_DENIED;
414         }
415
416         /* From the client / server challenges and md4 password, generate sess key */
417         creds_server_init(q_u->clnt_flgs.neg_flags,
418                         p->dc,
419                         &p->dc->clnt_chal,      /* Stored client chal. */
420                         &p->dc->srv_chal,       /* Stored server chal. */
421                         p->dc->mach_pw,
422                         &srv_chal_out); 
423
424         /* Check client credentials are valid. */
425         if (!creds_server_check(p->dc, &q_u->clnt_chal)) {
426                 DEBUG(0,("_net_auth2: creds_server_check failed. Rejecting auth "
427                         "request from client %s machine account %s\n",
428                         remote_machine, mach_acct ));
429                 return NT_STATUS_ACCESS_DENIED;
430         }
431
432         srv_flgs.neg_flags = 0x000001ff;
433
434         if (lp_server_schannel() != False) {
435                 srv_flgs.neg_flags |= NETLOGON_NEG_SCHANNEL;
436         }
437
438         /* set up the LSA AUTH 2 response */
439         init_net_r_auth_2(r_u, &srv_chal_out, &srv_flgs, NT_STATUS_OK);
440
441         fstrcpy(p->dc->mach_acct, mach_acct);
442         fstrcpy(p->dc->remote_machine, remote_machine);
443         fstrcpy(p->dc->domain, lp_workgroup() );
444
445         p->dc->authenticated = True;
446
447         /* Store off the state so we can continue after client disconnect. */
448         become_root();
449         secrets_store_schannel_session_info(p->mem_ctx,
450                                         remote_machine,
451                                         p->dc);
452         unbecome_root();
453
454         return r_u->status;
455 }
456
457 /*************************************************************************
458  _net_srv_pwset
459  *************************************************************************/
460
461 NTSTATUS _net_srv_pwset(pipes_struct *p, NET_Q_SRV_PWSET *q_u, NET_R_SRV_PWSET *r_u)
462 {
463         NTSTATUS status = NT_STATUS_ACCESS_DENIED;
464         fstring remote_machine;
465         struct samu *sampass=NULL;
466         BOOL ret = False;
467         unsigned char pwd[16];
468         int i;
469         uint32 acct_ctrl;
470         DOM_CRED cred_out;
471         const uchar *old_pw;
472
473         DEBUG(5,("_net_srv_pwset: %d\n", __LINE__));
474
475         /* We need the remote machine name for the creds lookup. */
476         rpcstr_pull(remote_machine,q_u->clnt_id.login.uni_comp_name.buffer,
477                     sizeof(remote_machine),q_u->clnt_id.login.uni_comp_name.uni_str_len*2,0);
478
479         if ( (lp_server_schannel() == True) && (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) ) {
480                 /* 'server schannel = yes' should enforce use of
481                    schannel, the client did offer it in auth2, but
482                    obviously did not use it. */
483                 DEBUG(0,("_net_srv_pwset: client %s not using schannel for netlogon\n",
484                         remote_machine ));
485                 return NT_STATUS_ACCESS_DENIED;
486         }
487
488         if (!p->dc) {
489                 /* Restore the saved state of the netlogon creds. */
490                 become_root();
491                 ret = secrets_restore_schannel_session_info(p->pipe_state_mem_ctx,
492                                                         remote_machine,
493                                                         &p->dc);
494                 unbecome_root();
495                 if (!ret) {
496                         return NT_STATUS_INVALID_HANDLE;
497                 }
498         }
499
500         if (!p->dc || !p->dc->authenticated) {
501                 return NT_STATUS_INVALID_HANDLE;
502         }
503
504         DEBUG(3,("_net_srv_pwset: Server Password Set by remote machine:[%s] on account [%s]\n",
505                         remote_machine, p->dc->mach_acct));
506         
507         /* Step the creds chain forward. */
508         if (!creds_server_step(p->dc, &q_u->clnt_id.cred, &cred_out)) {
509                 DEBUG(2,("_net_srv_pwset: creds_server_step failed. Rejecting auth "
510                         "request from client %s machine account %s\n",
511                         remote_machine, p->dc->mach_acct ));
512                 return NT_STATUS_INVALID_PARAMETER;
513         }
514
515         /* We must store the creds state after an update. */
516         become_root();
517         secrets_store_schannel_session_info(p->pipe_state_mem_ctx,
518                                                 remote_machine,
519                                                 p->dc);
520         pdb_init_sam(&sampass);
521         ret=pdb_getsampwnam(sampass, p->dc->mach_acct);
522         unbecome_root();
523
524         /* Ensure the account exists and is a machine account. */
525         
526         acct_ctrl = pdb_get_acct_ctrl(sampass);
527
528         if (!(ret 
529               && (acct_ctrl & ACB_WSTRUST ||
530                       acct_ctrl & ACB_SVRTRUST ||
531                       acct_ctrl & ACB_DOMTRUST))) {
532                 TALLOC_FREE(sampass);
533                 return NT_STATUS_NO_SUCH_USER;
534         }
535         
536         if (pdb_get_acct_ctrl(sampass) & ACB_DISABLED) {
537                 TALLOC_FREE(sampass);
538                 return NT_STATUS_ACCOUNT_DISABLED;
539         }
540
541         /* Woah - what does this to to the credential chain ? JRA */
542         cred_hash3( pwd, q_u->pwd, p->dc->sess_key, 0);
543
544         DEBUG(100,("Server password set : new given value was :\n"));
545         for(i = 0; i < sizeof(pwd); i++)
546                 DEBUG(100,("%02X ", pwd[i]));
547         DEBUG(100,("\n"));
548
549         old_pw = pdb_get_nt_passwd(sampass);
550
551         if (old_pw && memcmp(pwd, old_pw, 16) == 0) {
552                 /* Avoid backend modificiations and other fun if the 
553                    client changed the password to the *same thing* */
554
555                 ret = True;
556         } else {
557
558                 /* LM password should be NULL for machines */
559                 if (!pdb_set_lanman_passwd(sampass, NULL, PDB_CHANGED)) {
560                         TALLOC_FREE(sampass);
561                         return NT_STATUS_NO_MEMORY;
562                 }
563                 
564                 if (!pdb_set_nt_passwd(sampass, pwd, PDB_CHANGED)) {
565                         TALLOC_FREE(sampass);
566                         return NT_STATUS_NO_MEMORY;
567                 }
568                 
569                 if (!pdb_set_pass_changed_now(sampass)) {
570                         TALLOC_FREE(sampass);
571                         /* Not quite sure what this one qualifies as, but this will do */
572                         return NT_STATUS_UNSUCCESSFUL; 
573                 }
574                 
575                 become_root();
576                 r_u->status = pdb_update_sam_account (sampass);
577                 unbecome_root();
578         }
579
580         /* set up the LSA Server Password Set response */
581         init_net_r_srv_pwset(r_u, &cred_out, status);
582
583         TALLOC_FREE(sampass);
584         return r_u->status;
585 }
586
587 /*************************************************************************
588  _net_sam_logoff:
589  *************************************************************************/
590
591 NTSTATUS _net_sam_logoff(pipes_struct *p, NET_Q_SAM_LOGOFF *q_u, NET_R_SAM_LOGOFF *r_u)
592 {
593         fstring remote_machine;
594
595         if ( (lp_server_schannel() == True) && (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) ) {
596                 /* 'server schannel = yes' should enforce use of
597                    schannel, the client did offer it in auth2, but
598                    obviously did not use it. */
599                 DEBUG(0,("_net_sam_logoff: client %s not using schannel for netlogon\n",
600                         get_remote_machine_name() ));
601                 return NT_STATUS_ACCESS_DENIED;
602         }
603
604
605         if (!get_valid_user_struct(p->vuid))
606                 return NT_STATUS_NO_SUCH_USER;
607
608         /* Get the remote machine name for the creds store. */
609         rpcstr_pull(remote_machine,q_u->sam_id.client.login.uni_comp_name.buffer,
610                     sizeof(remote_machine),q_u->sam_id.client.login.uni_comp_name.uni_str_len*2,0);
611
612         if (!p->dc) {
613                 /* Restore the saved state of the netlogon creds. */
614                 BOOL ret;
615
616                 become_root();
617                 ret = secrets_restore_schannel_session_info(p->pipe_state_mem_ctx,
618                                                 remote_machine,
619                                                 &p->dc);
620                 unbecome_root();
621                 if (!ret) {
622                         return NT_STATUS_INVALID_HANDLE;
623                 }
624         }
625
626         if (!p->dc || !p->dc->authenticated) {
627                 return NT_STATUS_INVALID_HANDLE;
628         }
629
630         r_u->buffer_creds = 1; /* yes, we have valid server credentials */
631
632         /* checks and updates credentials.  creates reply credentials */
633         if (!creds_server_step(p->dc, &q_u->sam_id.client.cred, &r_u->srv_creds)) {
634                 DEBUG(2,("_net_sam_logoff: creds_server_step failed. Rejecting auth "
635                         "request from client %s machine account %s\n",
636                         remote_machine, p->dc->mach_acct ));
637                 return NT_STATUS_INVALID_PARAMETER;
638         }
639
640         /* We must store the creds state after an update. */
641         become_root();
642         secrets_store_schannel_session_info(p->pipe_state_mem_ctx,
643                                         remote_machine,
644                                         p->dc);
645         unbecome_root();
646
647         r_u->status = NT_STATUS_OK;
648         return r_u->status;
649 }
650
651 /*******************************************************************
652  gets a domain user's groups from their already-calculated NT_USER_TOKEN
653  ********************************************************************/
654
655 static NTSTATUS nt_token_to_group_list(TALLOC_CTX *mem_ctx,
656                                        const DOM_SID *domain_sid,
657                                        size_t num_sids,
658                                        const DOM_SID *sids,
659                                        int *numgroups, DOM_GID **pgids) 
660 {
661         int i;
662
663         *numgroups=0;
664         *pgids = NULL;
665
666         for (i=0; i<num_sids; i++) {
667                 DOM_GID gid;
668                 if (!sid_peek_check_rid(domain_sid, &sids[i], &gid.g_rid)) {
669                         continue;
670                 }
671                 gid.attr = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
672                             SE_GROUP_ENABLED);
673                 ADD_TO_ARRAY(mem_ctx, DOM_GID, gid, pgids, numgroups);
674                 if (*pgids == NULL) {
675                         return NT_STATUS_NO_MEMORY;
676                 }
677         }
678         return NT_STATUS_OK;
679 }
680
681 /*************************************************************************
682  _net_sam_logon
683  *************************************************************************/
684
685 static NTSTATUS _net_sam_logon_internal(pipes_struct *p,
686                                         NET_Q_SAM_LOGON *q_u,
687                                         NET_R_SAM_LOGON *r_u,
688                                         BOOL process_creds)
689 {
690         NTSTATUS status = NT_STATUS_OK;
691         NET_USER_INFO_3 *usr_info = NULL;
692         NET_ID_INFO_CTR *ctr = q_u->sam_id.ctr;
693         UNISTR2 *uni_samlogon_user = NULL;
694         UNISTR2 *uni_samlogon_domain = NULL;
695         UNISTR2 *uni_samlogon_workstation = NULL;
696         fstring nt_username, nt_domain, nt_workstation;
697         auth_usersupplied_info *user_info = NULL;
698         auth_serversupplied_info *server_info = NULL;
699         struct samu *sampw;
700         struct auth_context *auth_context = NULL;
701          
702         if ( (lp_server_schannel() == True) && (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) ) {
703                 /* 'server schannel = yes' should enforce use of
704                    schannel, the client did offer it in auth2, but
705                    obviously did not use it. */
706                 DEBUG(0,("_net_sam_logon_internal: client %s not using schannel for netlogon\n",
707                         get_remote_machine_name() ));
708                 return NT_STATUS_ACCESS_DENIED;
709         }
710
711         usr_info = TALLOC_P(p->mem_ctx, NET_USER_INFO_3);
712         if (!usr_info) {
713                 return NT_STATUS_NO_MEMORY;
714         }
715
716         ZERO_STRUCTP(usr_info);
717
718         /* store the user information, if there is any. */
719         r_u->user = usr_info;
720         r_u->auth_resp = 1; /* authoritative response */
721         if (q_u->validation_level != 2 && q_u->validation_level != 3) {
722                 DEBUG(0,("_net_sam_logon: bad validation_level value %d.\n", (int)q_u->validation_level ));
723                 return NT_STATUS_ACCESS_DENIED;
724         }
725         /* We handle the return of USER_INFO_2 instead of 3 in the parse return. Sucks, I know... */
726         r_u->switch_value = q_u->validation_level; /* indicates type of validation user info */
727         r_u->buffer_creds = 1; /* Ensure we always return server creds. */
728  
729         if (!get_valid_user_struct(p->vuid))
730                 return NT_STATUS_NO_SUCH_USER;
731
732         if (process_creds) {
733                 fstring remote_machine;
734
735                 /* Get the remote machine name for the creds store. */
736                 /* Note this is the remote machine this request is coming from (member server),
737                    not neccessarily the workstation name the user is logging onto.
738                 */
739                 rpcstr_pull(remote_machine,q_u->sam_id.client.login.uni_comp_name.buffer,
740                     sizeof(remote_machine),q_u->sam_id.client.login.uni_comp_name.uni_str_len*2,0);
741
742                 if (!p->dc) {
743                         /* Restore the saved state of the netlogon creds. */
744                         BOOL ret;
745
746                         become_root();
747                         ret = secrets_restore_schannel_session_info(p->pipe_state_mem_ctx,
748                                         remote_machine,
749                                         &p->dc);
750                         unbecome_root();
751                         if (!ret) {
752                                 return NT_STATUS_INVALID_HANDLE;
753                         }
754                 }
755
756                 if (!p->dc || !p->dc->authenticated) {
757                         return NT_STATUS_INVALID_HANDLE;
758                 }
759
760                 /* checks and updates credentials.  creates reply credentials */
761                 if (!creds_server_step(p->dc, &q_u->sam_id.client.cred,  &r_u->srv_creds)) {
762                         DEBUG(2,("_net_sam_logon: creds_server_step failed. Rejecting auth "
763                                 "request from client %s machine account %s\n",
764                                 remote_machine, p->dc->mach_acct ));
765                         return NT_STATUS_INVALID_PARAMETER;
766                 }
767
768                 /* We must store the creds state after an update. */
769                 become_root();
770                 secrets_store_schannel_session_info(p->pipe_state_mem_ctx,
771                                         remote_machine,
772                                         p->dc);
773                 unbecome_root();
774         }
775
776         switch (q_u->sam_id.logon_level) {
777         case INTERACTIVE_LOGON_TYPE:
778                 uni_samlogon_user = &ctr->auth.id1.uni_user_name;
779                 uni_samlogon_domain = &ctr->auth.id1.uni_domain_name;
780
781                 uni_samlogon_workstation = &ctr->auth.id1.uni_wksta_name;
782             
783                 DEBUG(3,("SAM Logon (Interactive). Domain:[%s].  ", lp_workgroup()));
784                 break;
785         case NET_LOGON_TYPE:
786                 uni_samlogon_user = &ctr->auth.id2.uni_user_name;
787                 uni_samlogon_domain = &ctr->auth.id2.uni_domain_name;
788                 uni_samlogon_workstation = &ctr->auth.id2.uni_wksta_name;
789             
790                 DEBUG(3,("SAM Logon (Network). Domain:[%s].  ", lp_workgroup()));
791                 break;
792         default:
793                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
794                 return NT_STATUS_INVALID_INFO_CLASS;
795         } /* end switch */
796
797         rpcstr_pull(nt_username,uni_samlogon_user->buffer,sizeof(nt_username),uni_samlogon_user->uni_str_len*2,0);
798         rpcstr_pull(nt_domain,uni_samlogon_domain->buffer,sizeof(nt_domain),uni_samlogon_domain->uni_str_len*2,0);
799         rpcstr_pull(nt_workstation,uni_samlogon_workstation->buffer,sizeof(nt_workstation),uni_samlogon_workstation->uni_str_len*2,0);
800
801         DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
802         fstrcpy(current_user_info.smb_name, nt_username);
803         sub_set_smb_name(nt_username);
804      
805         DEBUG(5,("Attempting validation level %d for unmapped username %s.\n", q_u->sam_id.ctr->switch_value, nt_username));
806
807         status = NT_STATUS_OK;
808         
809         switch (ctr->switch_value) {
810         case NET_LOGON_TYPE:
811         {
812                 const char *wksname = nt_workstation;
813                 
814                 if (!NT_STATUS_IS_OK(status = make_auth_context_fixed(&auth_context, ctr->auth.id2.lm_chal))) {
815                         return status;
816                 }
817
818                 /* For a network logon, the workstation name comes in with two
819                  * backslashes in the front. Strip them if they are there. */
820
821                 if (*wksname == '\\') wksname++;
822                 if (*wksname == '\\') wksname++;
823
824                 /* Standard challenge/response authenticaion */
825                 if (!make_user_info_netlogon_network(&user_info, 
826                                                      nt_username, nt_domain, 
827                                                      wksname,
828                                                      ctr->auth.id2.param_ctrl,
829                                                      ctr->auth.id2.lm_chal_resp.buffer,
830                                                      ctr->auth.id2.lm_chal_resp.str_str_len,
831                                                      ctr->auth.id2.nt_chal_resp.buffer,
832                                                      ctr->auth.id2.nt_chal_resp.str_str_len)) {
833                         status = NT_STATUS_NO_MEMORY;
834                 }       
835                 break;
836         }
837         case INTERACTIVE_LOGON_TYPE:
838                 /* 'Interactive' authentication, supplies the password in its
839                    MD4 form, encrypted with the session key.  We will convert
840                    this to challenge/response for the auth subsystem to chew
841                    on */
842         {
843                 const uint8 *chal;
844                 
845                 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
846                         return status;
847                 }
848                 
849                 chal = auth_context->get_ntlm_challenge(auth_context);
850
851                 if (!make_user_info_netlogon_interactive(&user_info, 
852                                                          nt_username, nt_domain, 
853                                                          nt_workstation, 
854                                                          ctr->auth.id1.param_ctrl,
855                                                          chal,
856                                                          ctr->auth.id1.lm_owf.data, 
857                                                          ctr->auth.id1.nt_owf.data, 
858                                                          p->dc->sess_key)) {
859                         status = NT_STATUS_NO_MEMORY;
860                 }
861                 break;
862         }
863         default:
864                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
865                 return NT_STATUS_INVALID_INFO_CLASS;
866         } /* end switch */
867         
868         if ( NT_STATUS_IS_OK(status) ) {
869                 status = auth_context->check_ntlm_password(auth_context, 
870                         user_info, &server_info);
871         }
872
873         (auth_context->free)(&auth_context);    
874         free_user_info(&user_info);
875         
876         DEBUG(5, ("_net_sam_logon: check_password returned status %s\n", 
877                   nt_errstr(status)));
878
879         /* Check account and password */
880     
881         if (!NT_STATUS_IS_OK(status)) {
882                 /* If we don't know what this domain is, we need to 
883                    indicate that we are not authoritative.  This 
884                    allows the client to decide if it needs to try 
885                    a local user.  Fix by jpjanosi@us.ibm.com, #2976 */
886                 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) 
887                      && !strequal(nt_domain, get_global_sam_name())
888                      && !is_trusted_domain(nt_domain) )
889                         r_u->auth_resp = 0; /* We are not authoritative */
890
891                 TALLOC_FREE(server_info);
892                 return status;
893         }
894
895         if (server_info->guest) {
896                 /* We don't like guest domain logons... */
897                 DEBUG(5,("_net_sam_logon: Attempted domain logon as GUEST "
898                          "denied.\n"));
899                 TALLOC_FREE(server_info);
900                 return NT_STATUS_LOGON_FAILURE;
901         }
902
903         /* This is the point at which, if the login was successful, that
904            the SAM Local Security Authority should record that the user is
905            logged in to the domain.  */
906     
907         {
908                 DOM_GID *gids = NULL;
909                 const DOM_SID *user_sid = NULL;
910                 const DOM_SID *group_sid = NULL;
911                 DOM_SID domain_sid;
912                 uint32 user_rid, group_rid; 
913
914                 int num_gids = 0;
915                 pstring my_name;
916                 fstring user_sid_string;
917                 fstring group_sid_string;
918                 unsigned char user_session_key[16];
919                 unsigned char lm_session_key[16];
920                 unsigned char pipe_session_key[16];
921
922                 sampw = server_info->sam_account;
923
924                 /* set up pointer indicating user/password failed to be
925                  * found */
926                 usr_info->ptr_user_info = 0;
927
928                 user_sid = pdb_get_user_sid(sampw);
929                 group_sid = pdb_get_group_sid(sampw);
930
931                 sid_copy(&domain_sid, user_sid);
932                 sid_split_rid(&domain_sid, &user_rid);
933
934                 if (!sid_peek_check_rid(&domain_sid, group_sid, &group_rid)) {
935                         DEBUG(1, ("_net_sam_logon: user %s\\%s has user sid "
936                                   "%s\n but group sid %s.\n"
937                                   "The conflicting domain portions are not "
938                                   "supported for NETLOGON calls\n",         
939                                   pdb_get_domain(sampw),
940                                   pdb_get_username(sampw),
941                                   sid_to_string(user_sid_string, user_sid),
942                                   sid_to_string(group_sid_string, group_sid)));
943                         return NT_STATUS_UNSUCCESSFUL;
944                 }
945                 
946                 
947                 if(server_info->login_server) {
948                         pstrcpy(my_name, server_info->login_server);
949                 } else {
950                         pstrcpy(my_name, global_myname());
951                 }
952
953                 status = nt_token_to_group_list(p->mem_ctx, &domain_sid,
954                                                 server_info->num_sids,
955                                                 server_info->sids,
956                                                 &num_gids, &gids);
957
958                 if (!NT_STATUS_IS_OK(status)) {
959                         return status;
960                 }
961
962                 if (server_info->user_session_key.length) {
963                         memcpy(user_session_key,
964                                server_info->user_session_key.data, 
965                                MIN(sizeof(user_session_key),
966                                    server_info->user_session_key.length));
967                         if (process_creds) {
968                                 /* Get the pipe session key from the creds. */
969                                 memcpy(pipe_session_key, p->dc->sess_key, 16);
970                         } else {
971                                 /* Get the pipe session key from the schannel. */
972                                 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL || p->auth.a_u.schannel_auth == NULL) {
973                                         return NT_STATUS_INVALID_HANDLE;
974                                 }
975                                 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->sess_key, 16);
976                         }
977                         SamOEMhash(user_session_key, pipe_session_key, 16);
978                         memset(pipe_session_key, '\0', 16);
979                 }
980                 if (server_info->lm_session_key.length) {
981                         memcpy(lm_session_key,
982                                server_info->lm_session_key.data, 
983                                MIN(sizeof(lm_session_key),
984                                    server_info->lm_session_key.length));
985                         if (process_creds) {
986                                 /* Get the pipe session key from the creds. */
987                                 memcpy(pipe_session_key, p->dc->sess_key, 16);
988                         } else {
989                                 /* Get the pipe session key from the schannel. */
990                                 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL || p->auth.a_u.schannel_auth == NULL) {
991                                         return NT_STATUS_INVALID_HANDLE;
992                                 }
993                                 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->sess_key, 16);
994                         }
995                         SamOEMhash(lm_session_key, pipe_session_key, 16);
996                         memset(pipe_session_key, '\0', 16);
997                 }
998                 
999                 init_net_user_info3(p->mem_ctx, usr_info, 
1000                                     user_rid,
1001                                     group_rid,   
1002                                     pdb_get_username(sampw),
1003                                     pdb_get_fullname(sampw),
1004                                     pdb_get_homedir(sampw),
1005                                     pdb_get_dir_drive(sampw),
1006                                     pdb_get_logon_script(sampw),
1007                                     pdb_get_profile_path(sampw),
1008                                     pdb_get_logon_time(sampw),
1009                                     get_time_t_max(),
1010                                     get_time_t_max(),
1011                                     pdb_get_pass_last_set_time(sampw),
1012                                     pdb_get_pass_can_change_time(sampw),
1013                                     pdb_get_pass_must_change_time(sampw),
1014                                     
1015                                     0, /* logon_count */
1016                                     0, /* bad_pw_count */
1017                                     num_gids,    /* uint32 num_groups */
1018                                     gids    , /* DOM_GID *gids */
1019                                     0x20    , /* uint32 user_flgs (?) */
1020                                     server_info->user_session_key.length ? user_session_key : NULL,
1021                                     server_info->lm_session_key.length ? lm_session_key : NULL,
1022                                     my_name     , /* char *logon_srv */
1023                                     pdb_get_domain(sampw),
1024                                     &domain_sid);     /* DOM_SID *dom_sid */  
1025                 ZERO_STRUCT(user_session_key);
1026                 ZERO_STRUCT(lm_session_key);
1027         }
1028         TALLOC_FREE(server_info);
1029         return status;
1030 }
1031
1032 /*************************************************************************
1033  _net_sam_logon
1034  *************************************************************************/
1035
1036 NTSTATUS _net_sam_logon(pipes_struct *p, NET_Q_SAM_LOGON *q_u, NET_R_SAM_LOGON *r_u)
1037 {
1038         return _net_sam_logon_internal(p, q_u, r_u, True);
1039 }
1040  
1041 /*************************************************************************
1042  _net_sam_logon_ex - no credential chaining. Map into net sam logon.
1043  *************************************************************************/
1044
1045 NTSTATUS _net_sam_logon_ex(pipes_struct *p, NET_Q_SAM_LOGON_EX *q_u, NET_R_SAM_LOGON_EX *r_u)
1046 {
1047         NET_Q_SAM_LOGON q;
1048         NET_R_SAM_LOGON r;
1049
1050         ZERO_STRUCT(q);
1051         ZERO_STRUCT(r);
1052
1053         /* Only allow this if the pipe is protected. */
1054         if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1055                 DEBUG(0,("_net_sam_logon_ex: client %s not using schannel for netlogon\n",
1056                         get_remote_machine_name() ));
1057                 return NT_STATUS_INVALID_PARAMETER;
1058         }
1059
1060         /* Map a NET_Q_SAM_LOGON_EX to NET_Q_SAM_LOGON. */
1061         q.validation_level = q_u->validation_level;
1062
1063         /* Map a DOM_SAM_INFO_EX into a DOM_SAM_INFO with no creds. */
1064         q.sam_id.client.login = q_u->sam_id.client;
1065         q.sam_id.logon_level = q_u->sam_id.logon_level;
1066         q.sam_id.ctr = q_u->sam_id.ctr;
1067
1068         r_u->status = _net_sam_logon_internal(p, &q, &r, False);
1069
1070         if (!NT_STATUS_IS_OK(r_u->status)) {
1071                 return r_u->status;
1072         }
1073
1074         /* Map the NET_R_SAM_LOGON to NET_R_SAM_LOGON_EX. */
1075         r_u->switch_value = r.switch_value;
1076         r_u->user = r.user;
1077         r_u->auth_resp = r.auth_resp;
1078         r_u->flags = 0; /* FIXME ! */
1079         return r_u->status;
1080 }
1081
1082 /*************************************************************************
1083  _ds_enum_dom_trusts
1084  *************************************************************************/
1085 #if 0   /* JERRY -- not correct */
1086 NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1087                              DS_R_ENUM_DOM_TRUSTS *r_u)
1088 {
1089         NTSTATUS status = NT_STATUS_OK;
1090
1091         /* TODO: According to MSDN, the can only be executed against a 
1092            DC or domain member running Windows 2000 or later.  Need
1093            to test against a standalone 2k server and see what it 
1094            does.  A windows 2000 DC includes its own domain in the 
1095            list.  --jerry */
1096
1097         return status;
1098 }
1099 #endif  /* JERRY */