more debug classess activated
[kai/samba-autobuild/.git] / source / 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  *  Copyirht  (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 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_RPC_SRV
31
32 extern pstring global_myname;
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
53 /*************************************************************************
54  net_reply_logon_ctrl:
55  *************************************************************************/
56
57 /* Some flag values reverse engineered from NLTEST.EXE */
58
59 #define LOGON_CTRL_IN_SYNC          0x00
60 #define LOGON_CTRL_REPL_NEEDED      0x01
61 #define LOGON_CTRL_REPL_IN_PROGRESS 0x02
62
63 NTSTATUS _net_logon_ctrl(pipes_struct *p, NET_Q_LOGON_CTRL *q_u, 
64                        NET_R_LOGON_CTRL *r_u)
65 {
66         uint32 flags = 0x0;
67         uint32 pdc_connection_status = 0x00; /* Maybe a win32 error code? */
68         
69         /* Setup the Logon Control response */
70
71         init_net_r_logon_ctrl(r_u, q_u->query_level, flags, 
72                               pdc_connection_status);
73
74         return r_u->status;
75 }
76
77 /****************************************************************************
78 Send a message to smbd to do a sam synchronisation
79 **************************************************************************/
80 static void send_sync_message(void)
81 {
82         TDB_CONTEXT *tdb;
83
84         tdb = tdb_open_log(lock_path("connections.tdb"), 0,
85                            TDB_DEFAULT, O_RDONLY, 0);
86
87         if (!tdb) {
88                 DEBUG(3, ("send_sync_message(): failed to open connections "
89                           "database\n"));
90                 return;
91         }
92
93         DEBUG(3, ("sending sam synchronisation message\n"));
94         
95         message_send_all(tdb, MSG_SMB_SAM_SYNC, NULL, 0, False, NULL);
96
97         tdb_close(tdb);
98 }
99
100 /*************************************************************************
101  net_reply_logon_ctrl2:
102  *************************************************************************/
103
104 NTSTATUS _net_logon_ctrl2(pipes_struct *p, NET_Q_LOGON_CTRL2 *q_u, NET_R_LOGON_CTRL2 *r_u)
105 {
106         uint32 flags = 0x0;
107         uint32 pdc_connection_status = 0x0;
108         uint32 logon_attempts = 0x0;
109         uint32 tc_status = ERROR_NO_LOGON_SERVERS;
110         char *trusted_domain = "test_domain";
111
112         DEBUG(0, ("*** net long ctrl2 %d, %d, %d\n",
113                   q_u->function_code, q_u->query_level, q_u->switch_value));
114
115         DEBUG(6,("_net_logon_ctrl2: %d\n", __LINE__));
116
117
118         /* set up the Logon Control2 response */
119         init_net_r_logon_ctrl2(r_u, q_u->query_level,
120                                flags, pdc_connection_status, logon_attempts,
121                                tc_status, trusted_domain);
122
123         if (lp_server_role() == ROLE_DOMAIN_BDC)
124                 send_sync_message();
125
126         DEBUG(6,("_net_logon_ctrl2: %d\n", __LINE__));
127
128         return r_u->status;
129 }
130
131 /*************************************************************************
132  net_reply_trust_dom_list:
133  *************************************************************************/
134
135 NTSTATUS _net_trust_dom_list(pipes_struct *p, NET_Q_TRUST_DOM_LIST *q_u, NET_R_TRUST_DOM_LIST *r_u)
136 {
137         char *trusted_domain = "test_domain";
138         uint32 num_trust_domains = 1;
139
140         DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
141
142         /* set up the Trusted Domain List response */
143         init_r_trust_dom(r_u, num_trust_domains, trusted_domain);
144
145         DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
146
147         return r_u->status;
148 }
149
150 /***********************************************************************************
151  init_net_r_srv_pwset:
152  ***********************************************************************************/
153
154 static void init_net_r_srv_pwset(NET_R_SRV_PWSET *r_s,
155                                  DOM_CRED *srv_cred, NTSTATUS status)  
156 {
157         DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
158
159         memcpy(&r_s->srv_cred, srv_cred, sizeof(r_s->srv_cred));
160         r_s->status = status;
161
162         DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
163 }
164
165 /******************************************************************
166  gets a machine password entry.  checks access rights of the host.
167  ******************************************************************/
168
169 static BOOL get_md4pw(char *md4pw, char *mach_acct)
170 {
171         SAM_ACCOUNT *sampass = NULL;
172         const uint8 *pass;
173         BOOL ret;
174         uint32 acct_ctrl;
175
176 #if 0
177     /*
178      * Currently this code is redundent as we already have a filter
179      * by hostname list. What this code really needs to do is to 
180      * get a hosts allowed/hosts denied list from the SAM database
181      * on a per user basis, and make the access decision there.
182      * I will leave this code here for now as a reminder to implement
183      * this at a later date. JRA.
184      */
185
186         if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
187                           client_name(), client_addr()))
188         {
189                 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
190                 return False;
191         }
192 #endif /* 0 */
193
194         if(!NT_STATUS_IS_OK(pdb_init_sam(&sampass)))
195                 return False;
196
197         /* JRA. This is ok as it is only used for generating the challenge. */
198         become_root();
199         ret=pdb_getsampwnam(sampass, mach_acct);
200         unbecome_root();
201  
202         if (ret==False) {
203                 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
204                 pdb_free_sam(&sampass);
205                 return False;
206         }
207
208         acct_ctrl = pdb_get_acct_ctrl(sampass);
209         if (!(acct_ctrl & ACB_DISABLED) &&
210             ((acct_ctrl & ACB_DOMTRUST) ||
211              (acct_ctrl & ACB_WSTRUST) ||
212              (acct_ctrl & ACB_SVRTRUST)) &&
213             ((pass=pdb_get_nt_passwd(sampass)) != NULL)) {
214                 memcpy(md4pw, pass, 16);
215                 dump_data(5, md4pw, 16);
216                 pdb_free_sam(&sampass);
217                 return True;
218         }
219         
220         DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
221         pdb_free_sam(&sampass);
222         return False;
223
224 }
225
226 /*************************************************************************
227  _net_req_chal
228  *************************************************************************/
229
230 NTSTATUS _net_req_chal(pipes_struct *p, NET_Q_REQ_CHAL *q_u, NET_R_REQ_CHAL *r_u)
231 {
232         NTSTATUS status = NT_STATUS_OK;
233
234         rpcstr_pull(p->dc.remote_machine,q_u->uni_logon_clnt.buffer,sizeof(fstring),q_u->uni_logon_clnt.uni_str_len*2,0);
235
236         /* create a server challenge for the client */
237         /* Set these to random values. */
238         generate_random_buffer(p->dc.srv_chal.data, 8, False);
239         
240         memcpy(p->dc.srv_cred.challenge.data, p->dc.srv_chal.data, 8);
241
242         memcpy(p->dc.clnt_chal.data          , q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
243         memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
244
245         memset((char *)p->dc.sess_key, '\0', sizeof(p->dc.sess_key));
246
247         p->dc.challenge_sent = True;
248         /* set up the LSA REQUEST CHALLENGE response */
249         init_net_r_req_chal(r_u, &p->dc.srv_chal, status);
250         
251         return status;
252 }
253
254 /*************************************************************************
255  init_net_r_auth:
256  *************************************************************************/
257
258 static void init_net_r_auth(NET_R_AUTH *r_a, DOM_CHAL *resp_cred, NTSTATUS status)
259 {
260         memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
261         r_a->status = status;
262 }
263
264 /*************************************************************************
265  _net_auth
266  *************************************************************************/
267
268 NTSTATUS _net_auth(pipes_struct *p, NET_Q_AUTH *q_u, NET_R_AUTH *r_u)
269 {
270         NTSTATUS status = NT_STATUS_OK;
271         DOM_CHAL srv_cred;
272         UTIME srv_time;
273         fstring mach_acct;
274
275         srv_time.time = 0;
276
277         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);
278
279         if (p->dc.challenge_sent && get_md4pw((char *)p->dc.md4pw, mach_acct)) {
280
281                 /* from client / server challenges and md4 password, generate sess key */
282                 cred_session_key(&p->dc.clnt_chal, &p->dc.srv_chal,
283                                  (char *)p->dc.md4pw, p->dc.sess_key);
284                 
285                 /* check that the client credentials are valid */
286                 if (cred_assert(&q_u->clnt_chal, p->dc.sess_key, &p->dc.clnt_cred.challenge, srv_time)) {
287                         
288                         /* create server challenge for inclusion in the reply */
289                         cred_create(p->dc.sess_key, &p->dc.srv_cred.challenge, srv_time, &srv_cred);
290                 
291                         /* copy the received client credentials for use next time */
292                         memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
293                         memcpy(p->dc.srv_cred .challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
294                         
295                         /* Save the machine account name. */
296                         fstrcpy(p->dc.mach_acct, mach_acct);
297                 
298                         p->dc.authenticated = True;
299
300                 } else {
301                         status = NT_STATUS_ACCESS_DENIED;
302                 }
303         } else {
304                 status = NT_STATUS_ACCESS_DENIED;
305         }
306         
307         /* set up the LSA AUTH response */
308         init_net_r_auth(r_u, &srv_cred, status);
309
310         return r_u->status;
311 }
312
313 /*************************************************************************
314  init_net_r_auth_2:
315  *************************************************************************/
316
317 static void init_net_r_auth_2(NET_R_AUTH_2 *r_a,
318                               DOM_CHAL *resp_cred, NEG_FLAGS *flgs, NTSTATUS status)
319 {
320         memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
321         memcpy(&r_a->srv_flgs, flgs, sizeof(r_a->srv_flgs));
322         r_a->status = status;
323 }
324
325 /*************************************************************************
326  _net_auth_2
327  *************************************************************************/
328
329 NTSTATUS _net_auth_2(pipes_struct *p, NET_Q_AUTH_2 *q_u, NET_R_AUTH_2 *r_u)
330 {
331         NTSTATUS status = NT_STATUS_OK;
332         DOM_CHAL srv_cred;
333         UTIME srv_time;
334         NEG_FLAGS srv_flgs;
335         fstring mach_acct;
336
337         srv_time.time = 0;
338
339         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);
340
341         if (p->dc.challenge_sent && get_md4pw((char *)p->dc.md4pw, mach_acct)) {
342                 
343                 /* from client / server challenges and md4 password, generate sess key */
344                 cred_session_key(&p->dc.clnt_chal, &p->dc.srv_chal,
345                                  (char *)p->dc.md4pw, p->dc.sess_key);
346                 
347                 /* check that the client credentials are valid */
348                 if (cred_assert(&q_u->clnt_chal, p->dc.sess_key, &p->dc.clnt_cred.challenge, srv_time)) {
349                         
350                         /* create server challenge for inclusion in the reply */
351                         cred_create(p->dc.sess_key, &p->dc.srv_cred.challenge, srv_time, &srv_cred);
352                         
353                         /* copy the received client credentials for use next time */
354                         memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
355                         memcpy(p->dc.srv_cred .challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
356                         
357                         /* Save the machine account name. */
358                         fstrcpy(p->dc.mach_acct, mach_acct);
359                         
360                         p->dc.authenticated = True;
361
362                 } else {
363                         status = NT_STATUS_ACCESS_DENIED;
364                 }
365         } else {
366                 status = NT_STATUS_ACCESS_DENIED;
367         }
368         
369         srv_flgs.neg_flags = 0x000001ff;
370
371         /* set up the LSA AUTH 2 response */
372         init_net_r_auth_2(r_u, &srv_cred, &srv_flgs, status);
373
374         return r_u->status;
375 }
376
377 /*************************************************************************
378  _net_srv_pwset
379  *************************************************************************/
380
381 NTSTATUS _net_srv_pwset(pipes_struct *p, NET_Q_SRV_PWSET *q_u, NET_R_SRV_PWSET *r_u)
382 {
383         NTSTATUS status = NT_STATUS_ACCESS_DENIED;
384         DOM_CRED srv_cred;
385         pstring workstation;
386         SAM_ACCOUNT *sampass=NULL;
387         BOOL ret = False;
388         unsigned char pwd[16];
389         int i;
390         uint32 acct_ctrl;
391
392         /* checks and updates credentials.  creates reply credentials */
393         if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->clnt_id.cred, &srv_cred)))
394                 return NT_STATUS_INVALID_HANDLE;
395
396         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
397
398         DEBUG(5,("_net_srv_pwset: %d\n", __LINE__));
399
400         rpcstr_pull(workstation,q_u->clnt_id.login.uni_comp_name.buffer,
401                     sizeof(workstation),q_u->clnt_id.login.uni_comp_name.uni_str_len*2,0);
402
403         DEBUG(3,("Server Password Set by Wksta:[%s] on account [%s]\n", workstation, p->dc.mach_acct));
404         
405         pdb_init_sam(&sampass);
406
407         become_root();
408         ret=pdb_getsampwnam(sampass, p->dc.mach_acct);
409         unbecome_root();
410
411         /* Ensure the account exists and is a machine account. */
412         
413         acct_ctrl = pdb_get_acct_ctrl(sampass);
414
415         if (!(ret 
416               && (acct_ctrl & ACB_WSTRUST ||
417                       acct_ctrl & ACB_SVRTRUST ||
418                       acct_ctrl & ACB_DOMTRUST))) {
419                 pdb_free_sam(&sampass);
420                 return NT_STATUS_NO_SUCH_USER;
421         }
422         
423         if (pdb_get_acct_ctrl(sampass) & ACB_DISABLED) {
424                 pdb_free_sam(&sampass);
425                 return NT_STATUS_ACCOUNT_DISABLED;
426         }
427
428         DEBUG(100,("Server password set : new given value was :\n"));
429         for(i = 0; i < 16; i++)
430                 DEBUG(100,("%02X ", q_u->pwd[i]));
431         DEBUG(100,("\n"));
432
433         cred_hash3( pwd, q_u->pwd, p->dc.sess_key, 0);
434
435         /* lies!  nt and lm passwords are _not_ the same: don't care */
436         if (!pdb_set_lanman_passwd (sampass, pwd)) {
437                 pdb_free_sam(&sampass);
438                 return NT_STATUS_NO_MEMORY;
439         }
440
441         if (!pdb_set_nt_passwd     (sampass, pwd)) {
442                 pdb_free_sam(&sampass);
443                 return NT_STATUS_NO_MEMORY;
444         }
445
446         if (!pdb_set_pass_changed_now     (sampass)) {
447                 pdb_free_sam(&sampass);
448                 /* Not quite sure what this one qualifies as, but this will do */
449                 return NT_STATUS_UNSUCCESSFUL; 
450         }
451  
452         become_root();
453         ret = pdb_update_sam_account (sampass);
454         unbecome_root();
455  
456         if (ret)
457                 status = NT_STATUS_OK;
458
459         /* set up the LSA Server Password Set response */
460         init_net_r_srv_pwset(r_u, &srv_cred, status);
461
462         pdb_free_sam(&sampass);
463         return r_u->status;
464 }
465
466
467 /*************************************************************************
468  _net_sam_logoff:
469  *************************************************************************/
470
471 NTSTATUS _net_sam_logoff(pipes_struct *p, NET_Q_SAM_LOGOFF *q_u, NET_R_SAM_LOGOFF *r_u)
472 {
473         DOM_CRED srv_cred;
474
475         if (!get_valid_user_struct(p->vuid))
476                 return NT_STATUS_NO_SUCH_USER;
477
478         /* checks and updates credentials.  creates reply credentials */
479         if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, 
480                                                      &q_u->sam_id.client.cred, &srv_cred)))
481                 return NT_STATUS_INVALID_HANDLE;
482
483         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
484
485         /* XXXX maybe we want to say 'no', reject the client's credentials */
486         r_u->buffer_creds = 1; /* yes, we have valid server credentials */
487         memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
488
489         r_u->status = NT_STATUS_OK;
490
491         return r_u->status;
492 }
493
494
495 /*************************************************************************
496  _net_sam_logon
497  *************************************************************************/
498
499 NTSTATUS _net_sam_logon(pipes_struct *p, NET_Q_SAM_LOGON *q_u, NET_R_SAM_LOGON *r_u)
500 {
501         NTSTATUS status = NT_STATUS_OK;
502         NET_USER_INFO_3 *usr_info = NULL;
503         NET_ID_INFO_CTR *ctr = q_u->sam_id.ctr;
504         DOM_CRED srv_cred;
505         UNISTR2 *uni_samlogon_user = NULL;
506         UNISTR2 *uni_samlogon_domain = NULL;
507         UNISTR2 *uni_samlogon_workstation = NULL;
508         fstring nt_username, nt_domain, nt_workstation;
509         auth_usersupplied_info *user_info = NULL;
510         auth_serversupplied_info *server_info = NULL;
511         extern userdom_struct current_user_info;
512         SAM_ACCOUNT *sampw;
513                 
514         usr_info = (NET_USER_INFO_3 *)talloc(p->mem_ctx, sizeof(NET_USER_INFO_3));
515         if (!usr_info)
516                 return NT_STATUS_NO_MEMORY;
517
518         ZERO_STRUCTP(usr_info);
519
520         /* store the user information, if there is any. */
521         r_u->user = usr_info;
522         r_u->switch_value = 0; /* indicates no info */
523         r_u->auth_resp = 1; /* authoritative response */
524         r_u->switch_value = 3; /* indicates type of validation user info */
525  
526         if (!get_valid_user_struct(p->vuid))
527                 return NT_STATUS_NO_SUCH_USER;
528     
529         /* checks and updates credentials.  creates reply credentials */
530         if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->sam_id.client.cred, &srv_cred)))
531                 return NT_STATUS_INVALID_HANDLE;
532
533         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
534     
535         r_u->buffer_creds = 1; /* yes, we have valid server credentials */
536         memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
537
538         /* find the username */
539     
540         switch (q_u->sam_id.logon_level) {
541         case INTERACTIVE_LOGON_TYPE:
542                 uni_samlogon_user = &ctr->auth.id1.uni_user_name;
543                 uni_samlogon_domain = &ctr->auth.id1.uni_domain_name;
544
545                 uni_samlogon_workstation = &ctr->auth.id1.uni_wksta_name;
546             
547                 DEBUG(3,("SAM Logon (Interactive). Domain:[%s].  ", lp_workgroup()));
548                 break;
549         case NET_LOGON_TYPE:
550                 uni_samlogon_user = &ctr->auth.id2.uni_user_name;
551                 uni_samlogon_domain = &ctr->auth.id2.uni_domain_name;
552                 uni_samlogon_workstation = &ctr->auth.id2.uni_wksta_name;
553             
554                 DEBUG(3,("SAM Logon (Network). Domain:[%s].  ", lp_workgroup()));
555                 break;
556         default:
557                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
558                 return NT_STATUS_INVALID_INFO_CLASS;
559         } /* end switch */
560
561         /* check username exists */
562
563         rpcstr_pull(nt_username,uni_samlogon_user->buffer,sizeof(nt_username),uni_samlogon_user->uni_str_len*2,0);
564         rpcstr_pull(nt_domain,uni_samlogon_domain->buffer,sizeof(nt_domain),uni_samlogon_domain->uni_str_len*2,0);
565         rpcstr_pull(nt_workstation,uni_samlogon_workstation->buffer,sizeof(nt_workstation),uni_samlogon_workstation->uni_str_len*2,0);
566
567         DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, 
568                  nt_workstation, nt_domain));
569         
570         pstrcpy(current_user_info.smb_name, nt_username);
571      
572         /*
573          * Convert to a UNIX username.
574          */
575
576         DEBUG(5,("Attempting validation level %d for unmapped username %s.\n", q_u->sam_id.ctr->switch_value, nt_username));
577
578         switch (ctr->switch_value) {
579         case NET_LOGON_TYPE:
580         {
581                 struct auth_context *auth_context = NULL;
582                 if (!NT_STATUS_IS_OK(status = make_auth_context_fixed(&auth_context, ctr->auth.id2.lm_chal))) {
583                         return status;
584                 }
585
586                 /* Standard challenge/response authenticaion */
587                 if (!make_user_info_netlogon_network(&user_info, 
588                                                      nt_username, nt_domain, 
589                                                      nt_workstation, 
590                                                      ctr->auth.id2.lm_chal_resp.buffer,
591                                                      ctr->auth.id2.lm_chal_resp.str_str_len,
592                                                      ctr->auth.id2.nt_chal_resp.buffer,
593                                                      ctr->auth.id2.nt_chal_resp.str_str_len)) {
594                         status = NT_STATUS_NO_MEMORY;
595                 } else {
596                         status = auth_context->check_ntlm_password(auth_context, user_info, &server_info);
597                 }
598                 (auth_context->free)(&auth_context);
599                         
600                 break;
601         }
602         case INTERACTIVE_LOGON_TYPE:
603                 /* 'Interactive' autheticaion, supplies the password in its
604                    MD4 form, encrypted with the session key.  We will
605                    convert this to chellange/responce for the auth
606                    subsystem to chew on */
607         {
608                 struct auth_context *auth_context = NULL;
609                 const uint8 *chal;
610                 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
611                         return status;
612                 }
613                 
614                 chal = auth_context->get_ntlm_challenge(auth_context);
615
616                 if (!make_user_info_netlogon_interactive(&user_info, 
617                                                          nt_username, nt_domain, 
618                                                          nt_workstation, chal,
619                                                          ctr->auth.id1.lm_owf.data, 
620                                                          ctr->auth.id1.nt_owf.data, 
621                                                          p->dc.sess_key)) {
622                         status = NT_STATUS_NO_MEMORY;
623                 } else {
624                         status = auth_context->check_ntlm_password(auth_context, user_info, &server_info);
625                 }
626
627                 (auth_context->free)(&auth_context);
628
629                 break;
630         }
631         default:
632                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
633                 return NT_STATUS_INVALID_INFO_CLASS;
634         } /* end switch */
635         
636         free_user_info(&user_info);
637         
638         DEBUG(5, ("_net_sam_logon: check_password returned status %s\n", 
639                   nt_errstr(status)));
640
641         /* Check account and password */
642     
643         if (!NT_STATUS_IS_OK(status)) {
644                 free_server_info(&server_info);
645                 return status;
646         }
647
648         if (server_info->guest) {
649                 /* We don't like guest domain logons... */
650                 DEBUG(5,("_net_sam_logon: Attempted domain logon as GUEST denied.\n"));
651                 free_server_info(&server_info);
652                 return NT_STATUS_LOGON_FAILURE;
653         }
654
655         /* This is the point at which, if the login was successful, that
656            the SAM Local Security Authority should record that the user is
657            logged in to the domain.  */
658     
659         {
660                 DOM_GID *gids = NULL;
661                 const DOM_SID *user_sid = NULL;
662                 const DOM_SID *group_sid = NULL;
663                 DOM_SID domain_sid;
664                 uint32 user_rid, group_rid; 
665
666                 int num_gids = 0;
667                 pstring my_name;
668                 fstring user_sid_string;
669                 fstring group_sid_string;
670                 
671                 sampw = server_info->sam_account;
672
673                 /* set up pointer indicating user/password failed to be found */
674                 usr_info->ptr_user_info = 0;
675
676                 user_sid = pdb_get_user_sid(sampw);
677                 group_sid = pdb_get_group_sid(sampw);
678
679                 sid_copy(&domain_sid, user_sid);
680                 sid_split_rid(&domain_sid, &user_rid);
681
682                 if (!sid_peek_check_rid(&domain_sid, group_sid, &group_rid)) {
683                         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",        
684                                   pdb_get_domain(sampw), pdb_get_username(sampw),
685                                   sid_to_string(user_sid_string, user_sid),
686                                   sid_to_string(group_sid_string, group_sid)));
687                         return NT_STATUS_UNSUCCESSFUL;
688                 }
689                 
690                 pstrcpy(my_name, global_myname);
691                 strupper(my_name);
692
693                 /*
694                  * This is the point at which we get the group
695                  * database - we should be getting the gid_t list
696                  * from /etc/group and then turning the uids into
697                  * rids and then into machine sids for this user.
698                  * JRA.
699                  */
700
701                 gids = NULL;
702                 get_domain_user_groups(p->mem_ctx, &num_gids, &gids, server_info->sam_account);
703
704                 init_net_user_info3(p->mem_ctx, usr_info, 
705                                     user_rid,
706                                     group_rid,
707                                     
708                                     pdb_get_username(sampw),
709                                     pdb_get_fullname(sampw),
710                                     pdb_get_homedir(sampw),
711                                     pdb_get_dirdrive(sampw),
712                                     pdb_get_logon_script(sampw),
713                                     pdb_get_profile_path(sampw),
714                                     pdb_get_logon_time(sampw),
715                                     pdb_get_logoff_time(sampw),
716                                     pdb_get_kickoff_time(sampw),
717                                     pdb_get_pass_last_set_time(sampw),
718                                     pdb_get_pass_can_change_time(sampw),
719                                     pdb_get_pass_must_change_time(sampw),
720                                     
721                                     0, /* logon_count */
722                                     0, /* bad_pw_count */
723                                     num_gids,    /* uint32 num_groups */
724                                     gids    , /* DOM_GID *gids */
725                                     0x20    , /* uint32 user_flgs (?) */
726                                     NULL, /* uchar sess_key[16] */
727                                     my_name     , /* char *logon_srv */
728                                     pdb_get_domain(sampw),
729                                     &domain_sid,     /* DOM_SID *dom_sid */  
730                                     /* Should be users domain sid, not servers - for trusted domains */
731                                   
732                                     NULL); /* char *other_sids */
733         }
734         free_server_info(&server_info);
735         return status;
736 }
737
738