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