r7139: trying to reduce the number of diffs between trunk and 3.0; changing version...
[abartlet/samba.git/.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 struct dcinfo last_dcinfo;
30 extern userdom_struct current_user_info;
31
32 #undef DBGC_CLASS
33 #define DBGC_CLASS DBGC_RPC_SRV
34
35 /*************************************************************************
36  init_net_r_req_chal:
37  *************************************************************************/
38
39 static void init_net_r_req_chal(NET_R_REQ_CHAL *r_c,
40                                 DOM_CHAL *srv_chal, NTSTATUS status)
41 {
42         DEBUG(6,("init_net_r_req_chal: %d\n", __LINE__));
43         memcpy(r_c->srv_chal.data, srv_chal->data, sizeof(srv_chal->data));
44         r_c->status = status;
45 }
46
47 /*************************************************************************
48  error messages cropping up when using nltest.exe...
49  *************************************************************************/
50
51 #define ERROR_NO_SUCH_DOMAIN   0x54b
52 #define ERROR_NO_LOGON_SERVERS 0x51f
53 #define NO_ERROR               0x0
54
55 /*************************************************************************
56  net_reply_logon_ctrl:
57  *************************************************************************/
58
59 NTSTATUS _net_logon_ctrl(pipes_struct *p, NET_Q_LOGON_CTRL *q_u, 
60                        NET_R_LOGON_CTRL *r_u)
61 {
62         uint32 flags = 0x0;
63         uint32 pdc_connection_status = 0x00; /* Maybe a win32 error code? */
64         
65         /* Setup the Logon Control response */
66
67         init_net_r_logon_ctrl(r_u, q_u->query_level, flags, 
68                               pdc_connection_status);
69
70         return r_u->status;
71 }
72
73 /****************************************************************************
74 Send a message to smbd to do a sam synchronisation
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         SAM_ACCOUNT *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                 pdb_free_sam(&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                 pdb_free_sam(&sampass);
255                 return True;
256         }
257         
258         DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
259         pdb_free_sam(&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         NTSTATUS status = NT_STATUS_OK;
271
272         rpcstr_pull(p->dc.remote_machine,q_u->uni_logon_clnt.buffer,sizeof(fstring),q_u->uni_logon_clnt.uni_str_len*2,0);
273
274         /* create a server challenge for the client */
275         /* Set these to random values. */
276         generate_random_buffer(p->dc.srv_chal.data, 8);
277         
278         memcpy(p->dc.srv_cred.challenge.data, p->dc.srv_chal.data, 8);
279
280         memcpy(p->dc.clnt_chal.data          , q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
281         memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
282
283         memset((char *)p->dc.sess_key, '\0', sizeof(p->dc.sess_key));
284
285         p->dc.challenge_sent = True;
286         /* set up the LSA REQUEST CHALLENGE response */
287         init_net_r_req_chal(r_u, &p->dc.srv_chal, status);
288         
289         return status;
290 }
291
292 /*************************************************************************
293  init_net_r_auth:
294  *************************************************************************/
295
296 static void init_net_r_auth(NET_R_AUTH *r_a, DOM_CHAL *resp_cred, NTSTATUS status)
297 {
298         memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
299         r_a->status = status;
300 }
301
302 /*************************************************************************
303  _net_auth
304  *************************************************************************/
305
306 NTSTATUS _net_auth(pipes_struct *p, NET_Q_AUTH *q_u, NET_R_AUTH *r_u)
307 {
308         NTSTATUS status = NT_STATUS_OK;
309         DOM_CHAL srv_cred;
310         UTIME srv_time;
311         fstring mach_acct;
312
313         srv_time.time = 0;
314
315         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);
316
317         if (p->dc.challenge_sent && get_md4pw((char *)p->dc.md4pw, mach_acct)) {
318
319                 /* from client / server challenges and md4 password, generate sess key */
320                 cred_session_key(&p->dc.clnt_chal, &p->dc.srv_chal,
321                                  p->dc.md4pw, p->dc.sess_key);
322                 
323                 /* check that the client credentials are valid */
324                 if (cred_assert(&q_u->clnt_chal, p->dc.sess_key, &p->dc.clnt_cred.challenge, srv_time)) {
325                         
326                         /* create server challenge for inclusion in the reply */
327                         cred_create(p->dc.sess_key, &p->dc.srv_cred.challenge, srv_time, &srv_cred);
328                 
329                         /* copy the received client credentials for use next time */
330                         memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
331                         memcpy(p->dc.srv_cred .challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
332                         
333                         /* Save the machine account name. */
334                         fstrcpy(p->dc.mach_acct, mach_acct);
335                 
336                         p->dc.authenticated = True;
337
338                 } else {
339                         status = NT_STATUS_ACCESS_DENIED;
340                 }
341         } else {
342                 status = NT_STATUS_ACCESS_DENIED;
343         }
344         
345         /* set up the LSA AUTH response */
346         init_net_r_auth(r_u, &srv_cred, status);
347
348         return r_u->status;
349 }
350
351 /*************************************************************************
352  init_net_r_auth_2:
353  *************************************************************************/
354
355 static void init_net_r_auth_2(NET_R_AUTH_2 *r_a,
356                               DOM_CHAL *resp_cred, NEG_FLAGS *flgs, NTSTATUS status)
357 {
358         memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
359         memcpy(&r_a->srv_flgs, flgs, sizeof(r_a->srv_flgs));
360         r_a->status = status;
361 }
362
363 /*************************************************************************
364  _net_auth_2
365  *************************************************************************/
366
367 NTSTATUS _net_auth_2(pipes_struct *p, NET_Q_AUTH_2 *q_u, NET_R_AUTH_2 *r_u)
368 {
369         NTSTATUS status = NT_STATUS_OK;
370         DOM_CHAL srv_cred;
371         UTIME srv_time;
372         NEG_FLAGS srv_flgs;
373         fstring mach_acct;
374
375         srv_time.time = 0;
376
377         if ( (lp_server_schannel() == True) &&
378              ((q_u->clnt_flgs.neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
379
380                 /* schannel must be used, but client did not offer it. */
381                 status = NT_STATUS_ACCESS_DENIED;
382         }
383
384         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);
385
386         if (p->dc.challenge_sent && get_md4pw((char *)p->dc.md4pw, mach_acct)) {
387                 
388                 /* from client / server challenges and md4 password, generate sess key */
389                 cred_session_key(&p->dc.clnt_chal, &p->dc.srv_chal,
390                                  p->dc.md4pw, p->dc.sess_key);
391                 
392                 /* check that the client credentials are valid */
393                 if (cred_assert(&q_u->clnt_chal, p->dc.sess_key, &p->dc.clnt_cred.challenge, srv_time)) {
394                         
395                         /* create server challenge for inclusion in the reply */
396                         cred_create(p->dc.sess_key, &p->dc.srv_cred.challenge, srv_time, &srv_cred);
397                         
398                         /* copy the received client credentials for use next time */
399                         memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
400                         memcpy(p->dc.srv_cred .challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
401                         
402                         /* Save the machine account name. */
403                         fstrcpy(p->dc.mach_acct, mach_acct);
404                         
405                         p->dc.authenticated = True;
406
407                 } else {
408                         status = NT_STATUS_ACCESS_DENIED;
409                 }
410         } else {
411                 status = NT_STATUS_ACCESS_DENIED;
412         }
413         
414         srv_flgs.neg_flags = 0x000001ff;
415
416         if (lp_server_schannel() != False) {
417                 srv_flgs.neg_flags |= NETLOGON_NEG_SCHANNEL;
418         }
419
420         /* set up the LSA AUTH 2 response */
421         init_net_r_auth_2(r_u, &srv_cred, &srv_flgs, status);
422
423         if (NT_STATUS_IS_OK(status)) {
424                 last_dcinfo = p->dc;
425         }
426
427         return r_u->status;
428 }
429
430 /*************************************************************************
431  _net_srv_pwset
432  *************************************************************************/
433
434 NTSTATUS _net_srv_pwset(pipes_struct *p, NET_Q_SRV_PWSET *q_u, NET_R_SRV_PWSET *r_u)
435 {
436         NTSTATUS status = NT_STATUS_ACCESS_DENIED;
437         DOM_CRED srv_cred;
438         pstring workstation;
439         SAM_ACCOUNT *sampass=NULL;
440         BOOL ret = False;
441         unsigned char pwd[16];
442         int i;
443         uint32 acct_ctrl;
444         const uchar *old_pw;
445
446         /* checks and updates credentials.  creates reply credentials */
447         if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->clnt_id.cred, &srv_cred)))
448                 return NT_STATUS_INVALID_HANDLE;
449
450         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
451
452         DEBUG(5,("_net_srv_pwset: %d\n", __LINE__));
453
454         rpcstr_pull(workstation,q_u->clnt_id.login.uni_comp_name.buffer,
455                     sizeof(workstation),q_u->clnt_id.login.uni_comp_name.uni_str_len*2,0);
456
457         DEBUG(3,("Server Password Set by Wksta:[%s] on account [%s]\n", workstation, p->dc.mach_acct));
458         
459         pdb_init_sam(&sampass);
460
461         become_root();
462         ret=pdb_getsampwnam(sampass, p->dc.mach_acct);
463         unbecome_root();
464
465         /* Ensure the account exists and is a machine account. */
466         
467         acct_ctrl = pdb_get_acct_ctrl(sampass);
468
469         if (!(ret 
470               && (acct_ctrl & ACB_WSTRUST ||
471                       acct_ctrl & ACB_SVRTRUST ||
472                       acct_ctrl & ACB_DOMTRUST))) {
473                 pdb_free_sam(&sampass);
474                 return NT_STATUS_NO_SUCH_USER;
475         }
476         
477         if (pdb_get_acct_ctrl(sampass) & ACB_DISABLED) {
478                 pdb_free_sam(&sampass);
479                 return NT_STATUS_ACCOUNT_DISABLED;
480         }
481
482         cred_hash3( pwd, q_u->pwd, p->dc.sess_key, 0);
483
484         DEBUG(100,("Server password set : new given value was :\n"));
485         for(i = 0; i < sizeof(pwd); i++)
486                 DEBUG(100,("%02X ", pwd[i]));
487         DEBUG(100,("\n"));
488
489         old_pw = pdb_get_nt_passwd(sampass);
490
491         if (old_pw && memcmp(pwd, old_pw, 16) == 0) {
492                 /* Avoid backend modificiations and other fun if the 
493                    client changed the password to the *same thing* */
494
495                 ret = True;
496         } else {
497
498                 /* LM password should be NULL for machines */
499                 if (!pdb_set_lanman_passwd (sampass, NULL, PDB_CHANGED)) {
500                         pdb_free_sam(&sampass);
501                         return NT_STATUS_NO_MEMORY;
502                 }
503                 
504                 if (!pdb_set_nt_passwd     (sampass, pwd, PDB_CHANGED)) {
505                         pdb_free_sam(&sampass);
506                         return NT_STATUS_NO_MEMORY;
507                 }
508                 
509                 if (!pdb_set_pass_changed_now     (sampass)) {
510                         pdb_free_sam(&sampass);
511                         /* Not quite sure what this one qualifies as, but this will do */
512                         return NT_STATUS_UNSUCCESSFUL; 
513                 }
514                 
515                 become_root();
516                 ret = pdb_update_sam_account (sampass);
517                 unbecome_root();
518         }
519         if (ret)
520                 status = NT_STATUS_OK;
521
522         /* set up the LSA Server Password Set response */
523         init_net_r_srv_pwset(r_u, &srv_cred, status);
524
525         pdb_free_sam(&sampass);
526         return r_u->status;
527 }
528
529
530 /*************************************************************************
531  _net_sam_logoff:
532  *************************************************************************/
533
534 NTSTATUS _net_sam_logoff(pipes_struct *p, NET_Q_SAM_LOGOFF *q_u, NET_R_SAM_LOGOFF *r_u)
535 {
536         DOM_CRED srv_cred;
537
538         if (!get_valid_user_struct(p->vuid))
539                 return NT_STATUS_NO_SUCH_USER;
540
541         /* checks and updates credentials.  creates reply credentials */
542         if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, 
543                                                      &q_u->sam_id.client.cred, &srv_cred)))
544                 return NT_STATUS_INVALID_HANDLE;
545
546         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
547
548         /* XXXX maybe we want to say 'no', reject the client's credentials */
549         r_u->buffer_creds = 1; /* yes, we have valid server credentials */
550         memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
551
552         r_u->status = NT_STATUS_OK;
553
554         return r_u->status;
555 }
556
557
558 /*************************************************************************
559  _net_sam_logon
560  *************************************************************************/
561
562 NTSTATUS _net_sam_logon(pipes_struct *p, NET_Q_SAM_LOGON *q_u, NET_R_SAM_LOGON *r_u)
563 {
564         NTSTATUS status = NT_STATUS_OK;
565         NET_USER_INFO_3 *usr_info = NULL;
566         NET_ID_INFO_CTR *ctr = q_u->sam_id.ctr;
567         DOM_CRED srv_cred;
568         UNISTR2 *uni_samlogon_user = NULL;
569         UNISTR2 *uni_samlogon_domain = NULL;
570         UNISTR2 *uni_samlogon_workstation = NULL;
571         fstring nt_username, nt_domain, nt_workstation;
572         auth_usersupplied_info *user_info = NULL;
573         auth_serversupplied_info *server_info = NULL;
574         SAM_ACCOUNT *sampw;
575         struct auth_context *auth_context = NULL;
576                 
577         usr_info = TALLOC_P(p->mem_ctx, NET_USER_INFO_3);
578         if (!usr_info)
579                 return NT_STATUS_NO_MEMORY;
580
581         ZERO_STRUCTP(usr_info);
582
583         /* store the user information, if there is any. */
584         r_u->user = usr_info;
585         r_u->switch_value = 0; /* indicates no info */
586         r_u->auth_resp = 1; /* authoritative response */
587         r_u->switch_value = 3; /* indicates type of validation user info */
588  
589         if (!get_valid_user_struct(p->vuid))
590                 return NT_STATUS_NO_SUCH_USER;
591
592
593         if ( (lp_server_schannel() == True) && (!p->netsec_auth_validated) ) {
594                 /* 'server schannel = yes' should enforce use of
595                    schannel, the client did offer it in auth2, but
596                    obviously did not use it. */
597                 return NT_STATUS_ACCESS_DENIED;
598         }
599
600         /* checks and updates credentials.  creates reply credentials */
601         if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->sam_id.client.cred, &srv_cred)))
602                 return NT_STATUS_INVALID_HANDLE;
603
604         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
605     
606         r_u->buffer_creds = 1; /* yes, we have valid server credentials */
607         memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
608
609         /* find the username */
610     
611         switch (q_u->sam_id.logon_level) {
612         case INTERACTIVE_LOGON_TYPE:
613                 uni_samlogon_user = &ctr->auth.id1.uni_user_name;
614                 uni_samlogon_domain = &ctr->auth.id1.uni_domain_name;
615
616                 uni_samlogon_workstation = &ctr->auth.id1.uni_wksta_name;
617             
618                 DEBUG(3,("SAM Logon (Interactive). Domain:[%s].  ", lp_workgroup()));
619                 break;
620         case NET_LOGON_TYPE:
621                 uni_samlogon_user = &ctr->auth.id2.uni_user_name;
622                 uni_samlogon_domain = &ctr->auth.id2.uni_domain_name;
623                 uni_samlogon_workstation = &ctr->auth.id2.uni_wksta_name;
624             
625                 DEBUG(3,("SAM Logon (Network). Domain:[%s].  ", lp_workgroup()));
626                 break;
627         default:
628                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
629                 return NT_STATUS_INVALID_INFO_CLASS;
630         } /* end switch */
631
632         rpcstr_pull(nt_username,uni_samlogon_user->buffer,sizeof(nt_username),uni_samlogon_user->uni_str_len*2,0);
633         rpcstr_pull(nt_domain,uni_samlogon_domain->buffer,sizeof(nt_domain),uni_samlogon_domain->uni_str_len*2,0);
634         rpcstr_pull(nt_workstation,uni_samlogon_workstation->buffer,sizeof(nt_workstation),uni_samlogon_workstation->uni_str_len*2,0);
635
636         DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, 
637                  nt_workstation, nt_domain));
638         
639         fstrcpy(current_user_info.smb_name, nt_username);
640         sub_set_smb_name(nt_username);
641      
642         DEBUG(5,("Attempting validation level %d for unmapped username %s.\n", q_u->sam_id.ctr->switch_value, nt_username));
643
644         status = NT_STATUS_OK;
645         
646         switch (ctr->switch_value) {
647         case NET_LOGON_TYPE:
648         {
649                 const char *wksname = nt_workstation;
650                 
651                 if (!NT_STATUS_IS_OK(status = make_auth_context_fixed(&auth_context, ctr->auth.id2.lm_chal))) {
652                         return status;
653                 }
654
655                 /* For a network logon, the workstation name comes in with two
656                  * backslashes in the front. Strip them if they are there. */
657
658                 if (*wksname == '\\') wksname++;
659                 if (*wksname == '\\') wksname++;
660
661                 /* Standard challenge/response authenticaion */
662                 if (!make_user_info_netlogon_network(&user_info, 
663                                                      nt_username, nt_domain, 
664                                                      wksname,
665                                                      ctr->auth.id2.lm_chal_resp.buffer,
666                                                      ctr->auth.id2.lm_chal_resp.str_str_len,
667                                                      ctr->auth.id2.nt_chal_resp.buffer,
668                                                      ctr->auth.id2.nt_chal_resp.str_str_len)) {
669                         status = NT_STATUS_NO_MEMORY;
670                 }       
671                 break;
672         }
673         case INTERACTIVE_LOGON_TYPE:
674                 /* 'Interactive' autheticaion, supplies the password in its
675                    MD4 form, encrypted with the session key.  We will
676                    convert this to chellange/responce for the auth
677                    subsystem to chew on */
678         {
679                 const uint8 *chal;
680                 
681                 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
682                         return status;
683                 }
684                 
685                 chal = auth_context->get_ntlm_challenge(auth_context);
686
687                 if (!make_user_info_netlogon_interactive(&user_info, 
688                                                          nt_username, nt_domain, 
689                                                          nt_workstation, chal,
690                                                          ctr->auth.id1.lm_owf.data, 
691                                                          ctr->auth.id1.nt_owf.data, 
692                                                          p->dc.sess_key)) {
693                         status = NT_STATUS_NO_MEMORY;
694                 }
695                 break;
696         }
697         default:
698                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
699                 return NT_STATUS_INVALID_INFO_CLASS;
700         } /* end switch */
701         
702         if ( NT_STATUS_IS_OK(status) ) {
703                 status = auth_context->check_ntlm_password(auth_context, 
704                         user_info, &server_info);
705         }
706
707         (auth_context->free)(&auth_context);    
708         free_user_info(&user_info);
709         
710         DEBUG(5, ("_net_sam_logon: check_password returned status %s\n", 
711                   nt_errstr(status)));
712
713         /* Check account and password */
714     
715         if (!NT_STATUS_IS_OK(status)) {
716                 free_server_info(&server_info);
717                 return status;
718         }
719
720         if (server_info->guest) {
721                 /* We don't like guest domain logons... */
722                 DEBUG(5,("_net_sam_logon: Attempted domain logon as GUEST denied.\n"));
723                 free_server_info(&server_info);
724                 return NT_STATUS_LOGON_FAILURE;
725         }
726
727         /* This is the point at which, if the login was successful, that
728            the SAM Local Security Authority should record that the user is
729            logged in to the domain.  */
730     
731         {
732                 DOM_GID *gids = NULL;
733                 const DOM_SID *user_sid = NULL;
734                 const DOM_SID *group_sid = NULL;
735                 DOM_SID domain_sid;
736                 uint32 user_rid, group_rid; 
737
738                 int num_gids = 0;
739                 pstring my_name;
740                 fstring user_sid_string;
741                 fstring group_sid_string;
742                 uchar user_session_key[16];
743                 uchar lm_session_key[16];
744                 uchar netlogon_sess_key[16];
745
746                 sampw = server_info->sam_account;
747
748                 /* set up pointer indicating user/password failed to be found */
749                 usr_info->ptr_user_info = 0;
750
751                 user_sid = pdb_get_user_sid(sampw);
752                 group_sid = pdb_get_group_sid(sampw);
753
754                 sid_copy(&domain_sid, user_sid);
755                 sid_split_rid(&domain_sid, &user_rid);
756
757                 if (!sid_peek_check_rid(&domain_sid, group_sid, &group_rid)) {
758                         DEBUG(1, ("_net_sam_logon: user %s\\%s has user sid %s\n but group sid %s.\nThe conflicting domain portions are not supported for NETLOGON calls\n",        
759                                   pdb_get_domain(sampw), pdb_get_username(sampw),
760                                   sid_to_string(user_sid_string, user_sid),
761                                   sid_to_string(group_sid_string, group_sid)));
762                         return NT_STATUS_UNSUCCESSFUL;
763                 }
764                 
765                 pstrcpy(my_name, global_myname());
766
767                 if (!NT_STATUS_IS_OK(status 
768                                      = nt_token_to_group_list(p->mem_ctx, 
769                                                               &domain_sid, 
770                                                               server_info->ptok, 
771                                                               &num_gids, 
772                                                               &gids))) {
773                         return status;
774                 }
775
776                 ZERO_STRUCT(netlogon_sess_key);
777                 memcpy(netlogon_sess_key, p->dc.sess_key, 8);
778                 if (server_info->user_session_key.length) {
779                         memcpy(user_session_key, server_info->user_session_key.data, 
780                                MIN(sizeof(user_session_key), server_info->user_session_key.length));
781                         SamOEMhash(user_session_key, netlogon_sess_key, 16);
782                 }
783                 if (server_info->lm_session_key.length) {
784                         memcpy(lm_session_key, server_info->lm_session_key.data, 
785                                MIN(sizeof(lm_session_key), server_info->lm_session_key.length));
786                         SamOEMhash(lm_session_key, netlogon_sess_key, 16);
787                 }
788                 ZERO_STRUCT(netlogon_sess_key);
789                 
790                 init_net_user_info3(p->mem_ctx, usr_info, 
791                                     user_rid,
792                                     group_rid,   
793                                     pdb_get_username(sampw),
794                                     pdb_get_fullname(sampw),
795                                     pdb_get_homedir(sampw),
796                                     pdb_get_dir_drive(sampw),
797                                     pdb_get_logon_script(sampw),
798                                     pdb_get_profile_path(sampw),
799                                     pdb_get_logon_time(sampw),
800                                     get_time_t_max(),
801                                     get_time_t_max(),
802                                     pdb_get_pass_last_set_time(sampw),
803                                     pdb_get_pass_can_change_time(sampw),
804                                     pdb_get_pass_must_change_time(sampw),
805                                     
806                                     0, /* logon_count */
807                                     0, /* bad_pw_count */
808                                     num_gids,    /* uint32 num_groups */
809                                     gids    , /* DOM_GID *gids */
810                                     0x20    , /* uint32 user_flgs (?) */
811                                     server_info->user_session_key.length ? user_session_key : NULL,
812                                     server_info->lm_session_key.length ? lm_session_key : NULL,
813                                     my_name     , /* char *logon_srv */
814                                     pdb_get_domain(sampw),
815                                     &domain_sid,     /* DOM_SID *dom_sid */  
816                                     /* Should be users domain sid, not servers - for trusted domains */
817                                   
818                                     NULL); /* char *other_sids */
819                 ZERO_STRUCT(user_session_key);
820                 ZERO_STRUCT(lm_session_key);
821         }
822         free_server_info(&server_info);
823         return status;
824 }
825
826 /*************************************************************************
827  _ds_enum_dom_trusts
828  *************************************************************************/
829 #if 0   /* JERRY -- not correct */
830 NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
831                              DS_R_ENUM_DOM_TRUSTS *r_u)
832 {
833         NTSTATUS status = NT_STATUS_OK;
834
835         /* TODO: According to MSDN, the can only be executed against a 
836            DC or domain member running Windows 2000 or later.  Need
837            to test against a standalone 2k server and see what it 
838            does.  A windows 2000 DC includes its own domain in the 
839            list.  --jerry */
840
841         return status;
842 }
843 #endif  /* JERRY */