Change the passdb interface to use allocated strings.
[ira/wip.git] / source3 / rpc_server / srv_netlog_nt.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  Version 1.9.
4  *  RPC Pipe client / server routines
5  *  Copyright (C) Andrew Tridgell              1992-1997,
6  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
7  *  Copyright (C) Paul Ashton                       1997.
8  *  Copyright (C) Jeremy Allison               1998-2001.
9  *  Copyirht  (C) Andrew Bartlett                   2001.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *  
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *  
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 /* This is the implementation of the netlogon pipe. */
27
28 #include "includes.h"
29
30 extern pstring global_myname;
31 extern DOM_SID global_sam_sid;
32
33 /*************************************************************************
34  init_net_r_req_chal:
35  *************************************************************************/
36
37 static void init_net_r_req_chal(NET_R_REQ_CHAL *r_c,
38                                 DOM_CHAL *srv_chal, NTSTATUS status)
39 {
40         DEBUG(6,("init_net_r_req_chal: %d\n", __LINE__));
41         memcpy(r_c->srv_chal.data, srv_chal->data, sizeof(srv_chal->data));
42         r_c->status = status;
43 }
44
45 /*************************************************************************
46  error messages cropping up when using nltest.exe...
47  *************************************************************************/
48
49 #define ERROR_NO_SUCH_DOMAIN   0x54b
50 #define ERROR_NO_LOGON_SERVERS 0x51f
51
52 /*************************************************************************
53  net_reply_logon_ctrl:
54  *************************************************************************/
55
56 /* Some flag values reverse engineered from NLTEST.EXE */
57
58 #define LOGON_CTRL_IN_SYNC          0x00
59 #define LOGON_CTRL_REPL_NEEDED      0x01
60 #define LOGON_CTRL_REPL_IN_PROGRESS 0x02
61
62 NTSTATUS _net_logon_ctrl(pipes_struct *p, NET_Q_LOGON_CTRL *q_u, 
63                        NET_R_LOGON_CTRL *r_u)
64 {
65         uint32 flags = 0x0;
66         uint32 pdc_connection_status = 0x00; /* Maybe a win32 error code? */
67         
68         /* Setup the Logon Control response */
69
70         init_net_r_logon_ctrl(r_u, q_u->query_level, flags, 
71                               pdc_connection_status);
72
73         return r_u->status;
74 }
75
76 /****************************************************************************
77 Send a message to smbd to do a sam synchronisation
78 **************************************************************************/
79 static void send_sync_message(void)
80 {
81         TDB_CONTEXT *tdb;
82
83         tdb = tdb_open_log(lock_path("connections.tdb"), 0,
84                            TDB_DEFAULT, O_RDONLY, 0);
85
86         if (!tdb) {
87                 DEBUG(3, ("send_sync_message(): failed to open connections "
88                           "database\n"));
89                 return;
90         }
91
92         DEBUG(3, ("sending sam synchronisation message\n"));
93         
94         message_send_all(tdb, MSG_SMB_SAM_SYNC, NULL, 0, False, NULL);
95
96         tdb_close(tdb);
97 }
98
99 /*************************************************************************
100  net_reply_logon_ctrl2:
101  *************************************************************************/
102
103 NTSTATUS _net_logon_ctrl2(pipes_struct *p, NET_Q_LOGON_CTRL2 *q_u, NET_R_LOGON_CTRL2 *r_u)
104 {
105         uint32 flags = 0x0;
106         uint32 pdc_connection_status = 0x0;
107         uint32 logon_attempts = 0x0;
108         uint32 tc_status = ERROR_NO_LOGON_SERVERS;
109         char *trusted_domain = "test_domain";
110
111         DEBUG(0, ("*** net long ctrl2 %d, %d, %d\n",
112                   q_u->function_code, q_u->query_level, q_u->switch_value));
113
114         DEBUG(6,("_net_logon_ctrl2: %d\n", __LINE__));
115
116
117         /* set up the Logon Control2 response */
118         init_net_r_logon_ctrl2(r_u, q_u->query_level,
119                                flags, pdc_connection_status, logon_attempts,
120                                tc_status, trusted_domain);
121
122         if (lp_server_role() == ROLE_DOMAIN_BDC)
123                 send_sync_message();
124
125         DEBUG(6,("_net_logon_ctrl2: %d\n", __LINE__));
126
127         return r_u->status;
128 }
129
130 /*************************************************************************
131  net_reply_trust_dom_list:
132  *************************************************************************/
133
134 NTSTATUS _net_trust_dom_list(pipes_struct *p, NET_Q_TRUST_DOM_LIST *q_u, NET_R_TRUST_DOM_LIST *r_u)
135 {
136         char *trusted_domain = "test_domain";
137         uint32 num_trust_domains = 1;
138
139         DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
140
141         /* set up the Trusted Domain List response */
142         init_r_trust_dom(r_u, num_trust_domains, trusted_domain);
143
144         DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
145
146         return r_u->status;
147 }
148
149 /***********************************************************************************
150  init_net_r_srv_pwset:
151  ***********************************************************************************/
152
153 static void init_net_r_srv_pwset(NET_R_SRV_PWSET *r_s,
154                              DOM_CRED *srv_cred, NTSTATUS status)  
155 {
156         DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
157
158         memcpy(&r_s->srv_cred, srv_cred, sizeof(r_s->srv_cred));
159         r_s->status = status;
160
161         DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
162 }
163
164 /******************************************************************
165  gets a machine password entry.  checks access rights of the host.
166  ******************************************************************/
167
168 static BOOL get_md4pw(char *md4pw, char *mach_acct)
169 {
170         SAM_ACCOUNT *sampass = NULL;
171         const uint8 *pass;
172         BOOL ret;
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         if (!(pdb_get_acct_ctrl(sampass) & ACB_DISABLED) && ((pass=pdb_get_nt_passwd(sampass)) != NULL)) {
207                 memcpy(md4pw, pass, 16);
208                 dump_data(5, md4pw, 16);
209                 pdb_free_sam(&sampass);
210                 return True;
211         }
212         
213         DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
214         pdb_free_sam(&sampass);
215         return False;
216
217 }
218
219 /*************************************************************************
220  _net_req_chal
221  *************************************************************************/
222
223 NTSTATUS _net_req_chal(pipes_struct *p, NET_Q_REQ_CHAL *q_u, NET_R_REQ_CHAL *r_u)
224 {
225         NTSTATUS status = NT_STATUS_OK;
226         fstring mach_acct;
227
228         if (!get_valid_user_struct(p->vuid))
229                 return NT_STATUS_NO_SUCH_USER;
230
231         rpcstr_pull(mach_acct,q_u->uni_logon_clnt.buffer,sizeof(fstring),q_u->uni_logon_clnt.uni_str_len*2,0);
232
233         strlower(mach_acct);
234         fstrcat(mach_acct, "$");
235
236         if (get_md4pw((char *)p->dc.md4pw, mach_acct)) {
237                 /* copy the client credentials */
238                 memcpy(p->dc.clnt_chal.data          , q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
239                 memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
240
241                 /* create a server challenge for the client */
242                 /* Set these to random values. */
243                 generate_random_buffer(p->dc.srv_chal.data, 8, False);
244
245                 memcpy(p->dc.srv_cred.challenge.data, p->dc.srv_chal.data, 8);
246
247                 memset((char *)p->dc.sess_key, '\0', sizeof(p->dc.sess_key));
248
249                 /* from client / server challenges and md4 password, generate sess key */
250                 cred_session_key(&p->dc.clnt_chal, &p->dc.srv_chal,
251                                  (char *)p->dc.md4pw, p->dc.sess_key);
252
253                 /* Save the machine account name. */
254                 fstrcpy(p->dc.mach_acct, mach_acct);
255
256         } else {
257                 /* lkclXXXX take a guess at a good error message to return :-) */
258                 status = NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
259         }
260
261         /* set up the LSA REQUEST CHALLENGE response */
262         init_net_r_req_chal(r_u, &p->dc.srv_chal, status);
263
264         return r_u->status;
265 }
266
267 /*************************************************************************
268  init_net_r_auth:
269  *************************************************************************/
270
271 static void init_net_r_auth(NET_R_AUTH *r_a, DOM_CHAL *resp_cred, NTSTATUS status)
272 {
273         memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
274         r_a->status = status;
275 }
276
277 /*************************************************************************
278  _net_auth
279  *************************************************************************/
280
281 NTSTATUS _net_auth(pipes_struct *p, NET_Q_AUTH *q_u, NET_R_AUTH *r_u)
282 {
283         NTSTATUS status = NT_STATUS_OK;
284         DOM_CHAL srv_cred;
285         UTIME srv_time;
286
287         if (!get_valid_user_struct(p->vuid))
288                 return NT_STATUS_NO_SUCH_USER;
289
290         srv_time.time = 0;
291
292         /* check that the client credentials are valid */
293         if (cred_assert(&q_u->clnt_chal, p->dc.sess_key, &p->dc.clnt_cred.challenge, srv_time)) {
294
295                 /* create server challenge for inclusion in the reply */
296                 cred_create(p->dc.sess_key, &p->dc.srv_cred.challenge, srv_time, &srv_cred);
297
298                 /* copy the received client credentials for use next time */
299                 memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
300                 memcpy(p->dc.srv_cred .challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
301         } else {
302                 status = NT_STATUS_ACCESS_DENIED;
303         }
304
305         /* set up the LSA AUTH 2 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
334         if (!get_valid_user_struct(p->vuid))
335                 return NT_STATUS_NO_SUCH_USER;
336
337         srv_time.time = 0;
338
339         /* check that the client credentials are valid */
340         if (cred_assert(&q_u->clnt_chal, p->dc.sess_key, &p->dc.clnt_cred.challenge, srv_time)) {
341
342                 /* create server challenge for inclusion in the reply */
343                 cred_create(p->dc.sess_key, &p->dc.srv_cred.challenge, srv_time, &srv_cred);
344
345                 /* copy the received client credentials for use next time */
346                 memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
347                 memcpy(p->dc.srv_cred .challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
348         } else {
349                 status = NT_STATUS_ACCESS_DENIED;
350         }
351
352         srv_flgs.neg_flags = 0x000001ff;
353
354         /* set up the LSA AUTH 2 response */
355         init_net_r_auth_2(r_u, &srv_cred, &srv_flgs, status);
356
357         return r_u->status;
358 }
359
360 /*************************************************************************
361  _net_srv_pwset
362  *************************************************************************/
363
364 NTSTATUS _net_srv_pwset(pipes_struct *p, NET_Q_SRV_PWSET *q_u, NET_R_SRV_PWSET *r_u)
365 {
366         NTSTATUS status = NT_STATUS_WRONG_PASSWORD;
367         DOM_CRED srv_cred;
368         pstring mach_acct;
369         SAM_ACCOUNT *sampass=NULL;
370         BOOL ret = False;
371         unsigned char pwd[16];
372         int i;
373
374         if (!get_valid_user_struct(p->vuid))
375                 return NT_STATUS_NO_SUCH_USER;
376
377         /* checks and updates credentials.  creates reply credentials */
378         if (!deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->clnt_id.cred, &srv_cred))
379                 return NT_STATUS_INVALID_HANDLE;
380
381         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
382
383         DEBUG(5,("_net_srv_pwset: %d\n", __LINE__));
384
385         rpcstr_pull(mach_acct,q_u->clnt_id.login.uni_acct_name.buffer,
386                         sizeof(mach_acct),q_u->clnt_id.login.uni_acct_name.uni_str_len*2,0);
387
388         DEBUG(3,("Server Password Set Wksta:[%s]\n", mach_acct));
389         
390         /*
391          * Check the machine account name we're changing is the same
392          * as the one we've authenticated from. This prevents arbitrary
393          * machines changing other machine account passwords.
394          */
395
396         if (!strequal(mach_acct, p->dc.mach_acct)) {
397                 return NT_STATUS_ACCESS_DENIED;
398         }
399
400         pdb_init_sam(&sampass);
401
402         become_root();
403         ret=pdb_getsampwnam(sampass, mach_acct);
404         unbecome_root();
405
406         /* Ensure the account exists and is a machine account. */
407
408         if (ret==False || !(pdb_get_acct_ctrl(sampass) & ACB_WSTRUST)) {
409                 pdb_free_sam(&sampass);
410                 return NT_STATUS_NO_SUCH_USER;
411         }
412         
413         DEBUG(100,("Server password set : new given value was :\n"));
414         for(i = 0; i < 16; i++)
415                 DEBUG(100,("%02X ", q_u->pwd[i]));
416         DEBUG(100,("\n"));
417
418         cred_hash3( pwd, q_u->pwd, p->dc.sess_key, 0);
419
420         /* lies!  nt and lm passwords are _not_ the same: don't care */
421         if (!pdb_set_lanman_passwd (sampass, pwd)) {
422                 pdb_free_sam(&sampass);
423                 return NT_STATUS_NO_MEMORY;
424         }
425
426         if (!pdb_set_nt_passwd     (sampass, pwd)) {
427                 pdb_free_sam(&sampass);
428                 return NT_STATUS_NO_MEMORY;
429         }
430
431         if (!pdb_set_acct_ctrl     (sampass, ACB_WSTRUST)) {
432                 pdb_free_sam(&sampass);
433                 /* Not quite sure what this one qualifies as, but this will do */
434                 return NT_STATUS_NO_MEMORY; 
435         }
436  
437         if (!pdb_set_pass_changed_now     (sampass)) {
438                 pdb_free_sam(&sampass);
439                 /* Not quite sure what this one qualifies as, but this will do */
440                 return NT_STATUS_NO_MEMORY; 
441         }
442  
443         become_root();
444         ret = pdb_update_sam_account (sampass,False);
445         unbecome_root();
446  
447         if (ret)
448                 status = NT_STATUS_OK;
449
450         /* set up the LSA Server Password Set response */
451         init_net_r_srv_pwset(r_u, &srv_cred, status);
452
453         pdb_free_sam(&sampass);
454         return r_u->status;
455 }
456
457
458 /*************************************************************************
459  _net_sam_logoff:
460  *************************************************************************/
461
462 NTSTATUS _net_sam_logoff(pipes_struct *p, NET_Q_SAM_LOGOFF *q_u, NET_R_SAM_LOGOFF *r_u)
463 {
464         DOM_CRED srv_cred;
465
466         if (!get_valid_user_struct(p->vuid))
467                 return NT_STATUS_NO_SUCH_USER;
468
469         /* checks and updates credentials.  creates reply credentials */
470         if (!deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, 
471                         &q_u->sam_id.client.cred, &srv_cred))
472                 return NT_STATUS_INVALID_HANDLE;
473
474         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
475
476         /* XXXX maybe we want to say 'no', reject the client's credentials */
477         r_u->buffer_creds = 1; /* yes, we have valid server credentials */
478         memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
479
480         r_u->status = NT_STATUS_OK;
481
482         return r_u->status;
483 }
484
485
486 /*************************************************************************
487  _net_sam_logon
488  *************************************************************************/
489
490 NTSTATUS _net_sam_logon(pipes_struct *p, NET_Q_SAM_LOGON *q_u, NET_R_SAM_LOGON *r_u)
491 {
492         NTSTATUS status = NT_STATUS_OK;
493         NET_USER_INFO_3 *usr_info = NULL;
494         NET_ID_INFO_CTR *ctr = q_u->sam_id.ctr;
495         DOM_CRED srv_cred;
496         UNISTR2 *uni_samlogon_user = NULL;
497         UNISTR2 *uni_samlogon_domain = NULL;
498         UNISTR2 *uni_samlogon_workstation = NULL;
499         fstring nt_username, nt_domain, nt_workstation;
500         auth_usersupplied_info *user_info = NULL;
501         auth_serversupplied_info *server_info = NULL;
502         extern userdom_struct current_user_info;
503                 
504         usr_info = (NET_USER_INFO_3 *)talloc(p->mem_ctx, sizeof(NET_USER_INFO_3));
505         if (!usr_info)
506                 return NT_STATUS_NO_MEMORY;
507
508         ZERO_STRUCTP(usr_info);
509  
510         if (!get_valid_user_struct(p->vuid))
511                 return NT_STATUS_NO_SUCH_USER;
512     
513         /* checks and updates credentials.  creates reply credentials */
514         if (!deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->sam_id.client.cred, &srv_cred))
515                 return NT_STATUS_INVALID_HANDLE;
516         else
517                 memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
518     
519         r_u->buffer_creds = 1; /* yes, we have valid server credentials */
520         memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
521
522         /* store the user information, if there is any. */
523         r_u->user = usr_info;
524         r_u->switch_value = 0; /* indicates no info */
525         r_u->auth_resp = 1; /* authoritative response */
526         r_u->switch_value = 3; /* indicates type of validation user info */
527
528         /* find the username */
529     
530         switch (q_u->sam_id.logon_level) {
531         case INTERACTIVE_LOGON_TYPE:
532                 uni_samlogon_user = &ctr->auth.id1.uni_user_name;
533                 uni_samlogon_domain = &ctr->auth.id1.uni_domain_name;
534
535                 uni_samlogon_workstation = &ctr->auth.id1.uni_wksta_name;
536             
537                 DEBUG(3,("SAM Logon (Interactive). Domain:[%s].  ", lp_workgroup()));
538                 break;
539         case NET_LOGON_TYPE:
540                 uni_samlogon_user = &ctr->auth.id2.uni_user_name;
541                 uni_samlogon_domain = &ctr->auth.id2.uni_domain_name;
542                 uni_samlogon_workstation = &ctr->auth.id2.uni_wksta_name;
543             
544                 DEBUG(3,("SAM Logon (Network). Domain:[%s].  ", lp_workgroup()));
545                 break;
546         default:
547                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
548                 return NT_STATUS_INVALID_INFO_CLASS;
549         } /* end switch */
550
551         /* check username exists */
552
553         rpcstr_pull(nt_username,uni_samlogon_user->buffer,sizeof(nt_username),uni_samlogon_user->uni_str_len*2,0);
554         rpcstr_pull(nt_domain,uni_samlogon_domain->buffer,sizeof(nt_domain),uni_samlogon_domain->uni_str_len*2,0);
555         rpcstr_pull(nt_workstation,uni_samlogon_workstation->buffer,sizeof(nt_workstation),uni_samlogon_workstation->uni_str_len*2,0);
556
557         DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, 
558                  nt_workstation, nt_domain));
559         
560         pstrcpy(current_user_info.smb_name, nt_username);
561      
562         /*
563          * Convert to a UNIX username.
564          */
565
566         DEBUG(5,("Attempting validation level %d for unmapped username %s.\n", q_u->sam_id.ctr->switch_value, nt_username));
567
568         switch (ctr->switch_value) {
569         case NET_LOGON_TYPE:
570         {
571                 struct auth_context *auth_context = NULL;
572                 if (!NT_STATUS_IS_OK(status = make_auth_context_fixed(&auth_context, ctr->auth.id2.lm_chal))) {
573                         return status;
574                 }
575
576                 /* Standard challenge/response authenticaion */
577                 if (!make_user_info_netlogon_network(&user_info, 
578                                                      nt_username, nt_domain, 
579                                                      nt_workstation, 
580                                                      ctr->auth.id2.lm_chal_resp.buffer,
581                                                      ctr->auth.id2.lm_chal_resp.str_str_len,
582                                                      ctr->auth.id2.nt_chal_resp.buffer,
583                                                      ctr->auth.id2.nt_chal_resp.str_str_len)) {
584                         status = NT_STATUS_NO_MEMORY;
585                 } else {
586                         status = auth_context->check_ntlm_password(auth_context, user_info, &server_info);
587                 }
588                 (auth_context->free)(&auth_context);
589                         
590                 break;
591         }
592         case INTERACTIVE_LOGON_TYPE:
593                 /* 'Interactive' autheticaion, supplies the password in its
594                    MD4 form, encrypted with the session key.  We will
595                    convert this to chellange/responce for the auth
596                    subsystem to chew on */
597         {
598                 struct auth_context *auth_context = NULL;
599                 const uint8 *chal;
600                 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
601                         return status;
602                 }
603                 
604                 chal = auth_context->get_ntlm_challenge(auth_context);
605
606                 if (!make_user_info_netlogon_interactive(&user_info, 
607                                                          nt_username, nt_domain, 
608                                                          nt_workstation, chal,
609                                                          ctr->auth.id1.lm_owf.data, 
610                                                          ctr->auth.id1.nt_owf.data, 
611                                                          p->dc.sess_key)) {
612                         status = NT_STATUS_NO_MEMORY;
613                 } else {
614                         status = auth_context->check_ntlm_password(auth_context, user_info, &server_info);
615                 }
616
617                 (auth_context->free)(&auth_context);
618
619                 break;
620         }
621         default:
622                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
623                 return NT_STATUS_INVALID_INFO_CLASS;
624         } /* end switch */
625         
626         free_user_info(&user_info);
627         
628         DEBUG(5, ("_net_sam_logon: check_password returned status %s\n", 
629                   get_nt_error_msg(status)));
630
631         /* Check account and password */
632     
633         if (!NT_STATUS_IS_OK(status)) {
634                 free_server_info(&server_info);
635                 return status;
636         }
637
638         if (server_info->guest) {
639                 /* We don't like guest domain logons... */
640                 DEBUG(5,("_net_sam_logon: Attempted domain logon as GUEST denied.\n"));
641                 free_server_info(&server_info);
642                 return NT_STATUS_LOGON_FAILURE;
643         }
644
645         /* This is the point at which, if the login was successful, that
646            the SAM Local Security Authority should record that the user is
647            logged in to the domain.  */
648     
649         {
650                 DOM_GID *gids = NULL;
651                 int num_gids = 0;
652                 pstring my_name;
653                 pstring my_workgroup;
654         
655                 /* set up pointer indicating user/password failed to be found */
656                 usr_info->ptr_user_info = 0;
657         
658                 pstrcpy(my_workgroup, lp_workgroup());
659                 pstrcpy(my_name, global_myname);
660                 strupper(my_name);
661
662                 /*
663                  * This is the point at which we get the group
664                  * database - we should be getting the gid_t list
665                  * from /etc/group and then turning the uids into
666                  * rids and then into machine sids for this user.
667                  * JRA.
668                  */
669
670                 gids = NULL;
671                 get_domain_user_groups(p->mem_ctx, &num_gids, &gids, server_info->sam_account);
672         
673                 init_net_user_info3(p->mem_ctx, usr_info, server_info->sam_account,
674                             0, /* logon_count */
675                             0, /* bad_pw_count */
676                             num_gids,    /* uint32 num_groups */
677                             gids    , /* DOM_GID *gids */
678                             0x20    , /* uint32 user_flgs (?) */
679                             NULL, /* uchar sess_key[16] */
680                             my_name     , /* char *logon_srv */
681                             my_workgroup, /* char *logon_dom */
682                             &global_sam_sid,     /* DOM_SID *dom_sid */
683                             NULL); /* char *other_sids */
684
685         }
686         free_server_info(&server_info);
687         return status;
688 }
689
690