388d649c3c161d8a84c12c9e2c074848f6fe4673
[metze/samba/wip.git] / source3 / rpc_server / srv_netlog_nt.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-1997,
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6  *  Copyright (C) Paul Ashton                       1997.
7  *  Copyright (C) Jeremy Allison               1998-2001.
8  *  Copyright (C) Andrew Bartlett                   2001.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 /* This is the implementation of the netlogon pipe. */
26
27 #include "includes.h"
28
29 extern struct dcinfo last_dcinfo;
30 extern BOOL server_auth2_negotiated;
31 extern userdom_struct current_user_info;
32
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_RPC_SRV
35
36 /*************************************************************************
37  init_net_r_req_chal:
38  *************************************************************************/
39
40 static void init_net_r_req_chal(NET_R_REQ_CHAL *r_c,
41                                 DOM_CHAL *srv_chal, NTSTATUS status)
42 {
43         DEBUG(6,("init_net_r_req_chal: %d\n", __LINE__));
44         memcpy(r_c->srv_chal.data, srv_chal->data, sizeof(srv_chal->data));
45         r_c->status = status;
46 }
47
48 /*************************************************************************
49  error messages cropping up when using nltest.exe...
50  *************************************************************************/
51
52 #define ERROR_NO_SUCH_DOMAIN   0x54b
53 #define ERROR_NO_LOGON_SERVERS 0x51f
54 #define NO_ERROR               0x0
55
56 /*************************************************************************
57  net_reply_logon_ctrl:
58  *************************************************************************/
59
60 NTSTATUS _net_logon_ctrl(pipes_struct *p, NET_Q_LOGON_CTRL *q_u, 
61                        NET_R_LOGON_CTRL *r_u)
62 {
63         uint32 flags = 0x0;
64         uint32 pdc_connection_status = 0x00; /* Maybe a win32 error code? */
65         
66         /* Setup the Logon Control response */
67
68         init_net_r_logon_ctrl(r_u, q_u->query_level, flags, 
69                               pdc_connection_status);
70
71         return r_u->status;
72 }
73
74 /****************************************************************************
75 Send a message to smbd to do a sam synchronisation
76 **************************************************************************/
77 static void send_sync_message(void)
78 {
79         TDB_CONTEXT *tdb;
80
81         tdb = tdb_open_log(lock_path("connections.tdb"), 0,
82                            TDB_DEFAULT, O_RDONLY, 0);
83
84         if (!tdb) {
85                 DEBUG(3, ("send_sync_message(): failed to open connections "
86                           "database\n"));
87                 return;
88         }
89
90         DEBUG(3, ("sending sam synchronisation message\n"));
91         
92         message_send_all(tdb, MSG_SMB_SAM_SYNC, NULL, 0, False, NULL);
93
94         tdb_close(tdb);
95 }
96
97 /*************************************************************************
98  net_reply_logon_ctrl2:
99  *************************************************************************/
100
101 NTSTATUS _net_logon_ctrl2(pipes_struct *p, NET_Q_LOGON_CTRL2 *q_u, NET_R_LOGON_CTRL2 *r_u)
102 {
103         uint32 flags = 0x0;
104         uint32 pdc_connection_status = 0x0;
105         uint32 logon_attempts = 0x0;
106         uint32 tc_status;
107         fstring servername, domain, dc_name, dc_name2;
108         struct in_addr dc_ip;
109
110         /* this should be \\global_myname() */
111         unistr2_to_ascii(servername, &q_u->uni_server_name, sizeof(servername));
112
113         r_u->status = NT_STATUS_OK;
114         
115         tc_status = ERROR_NO_SUCH_DOMAIN;
116         fstrcpy( dc_name, "" );
117         
118         switch ( q_u->function_code ) {
119                 case NETLOGON_CONTROL_TC_QUERY:
120                         unistr2_to_ascii(domain, &q_u->info.info6.domain, sizeof(domain));
121                                 
122                         if ( !is_trusted_domain( domain ) )
123                                 break;
124                                 
125                         if ( !get_dc_name( domain, NULL, dc_name2, &dc_ip ) ) {
126                                 tc_status = ERROR_NO_LOGON_SERVERS;
127                                 break;
128                         }
129
130                         fstr_sprintf( dc_name, "\\\\%s", dc_name2 );
131                                 
132                         tc_status = NO_ERROR;
133                         
134                         break;
135                         
136                 case NETLOGON_CONTROL_REDISCOVER:
137                         unistr2_to_ascii(domain, &q_u->info.info6.domain, sizeof(domain));
138                                 
139                         if ( !is_trusted_domain( domain ) )
140                                 break;
141                                 
142                         if ( !get_dc_name( domain, NULL, dc_name2, &dc_ip ) ) {
143                                 tc_status = ERROR_NO_LOGON_SERVERS;
144                                 break;
145                         }
146
147                         fstr_sprintf( dc_name, "\\\\%s", dc_name2 );
148                                 
149                         tc_status = NO_ERROR;
150                         
151                         break;
152                         
153                 default:
154                         /* no idea what this should be */
155                         DEBUG(0,("_net_logon_ctrl2: unimplemented function level [%d]\n",
156                                 q_u->function_code));
157         }
158         
159         /* prepare the response */
160         
161         init_net_r_logon_ctrl2( r_u, q_u->query_level, flags, 
162                 pdc_connection_status, logon_attempts, tc_status, dc_name );
163
164         if (lp_server_role() == ROLE_DOMAIN_BDC)
165                 send_sync_message();
166
167         return r_u->status;
168 }
169
170 /*************************************************************************
171  net_reply_trust_dom_list:
172  *************************************************************************/
173
174 NTSTATUS _net_trust_dom_list(pipes_struct *p, NET_Q_TRUST_DOM_LIST *q_u, NET_R_TRUST_DOM_LIST *r_u)
175 {
176         const char *trusted_domain = "test_domain";
177         uint32 num_trust_domains = 1;
178
179         DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
180
181         /* set up the Trusted Domain List response */
182         init_r_trust_dom(r_u, num_trust_domains, trusted_domain);
183
184         DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
185
186         return r_u->status;
187 }
188
189 /***********************************************************************************
190  init_net_r_srv_pwset:
191  ***********************************************************************************/
192
193 static void init_net_r_srv_pwset(NET_R_SRV_PWSET *r_s,
194                                  DOM_CRED *srv_cred, NTSTATUS status)  
195 {
196         DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
197
198         memcpy(&r_s->srv_cred, srv_cred, sizeof(r_s->srv_cred));
199         r_s->status = status;
200
201         DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
202 }
203
204 /******************************************************************
205  gets a machine password entry.  checks access rights of the host.
206  ******************************************************************/
207
208 static BOOL get_md4pw(char *md4pw, char *mach_acct)
209 {
210         SAM_ACCOUNT *sampass = NULL;
211         const uint8 *pass;
212         BOOL ret;
213         uint32 acct_ctrl;
214
215 #if 0
216     /*
217      * Currently this code is redundent as we already have a filter
218      * by hostname list. What this code really needs to do is to 
219      * get a hosts allowed/hosts denied list from the SAM database
220      * on a per user basis, and make the access decision there.
221      * I will leave this code here for now as a reminder to implement
222      * this at a later date. JRA.
223      */
224
225         if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
226                           client_name(), client_addr()))
227         {
228                 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
229                 return False;
230         }
231 #endif /* 0 */
232
233         if(!NT_STATUS_IS_OK(pdb_init_sam(&sampass)))
234                 return False;
235
236         /* JRA. This is ok as it is only used for generating the challenge. */
237         become_root();
238         ret=pdb_getsampwnam(sampass, mach_acct);
239         unbecome_root();
240  
241         if (ret==False) {
242                 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
243                 pdb_free_sam(&sampass);
244                 return False;
245         }
246
247         acct_ctrl = pdb_get_acct_ctrl(sampass);
248         if (!(acct_ctrl & ACB_DISABLED) &&
249             ((acct_ctrl & ACB_DOMTRUST) ||
250              (acct_ctrl & ACB_WSTRUST) ||
251              (acct_ctrl & ACB_SVRTRUST)) &&
252             ((pass=pdb_get_nt_passwd(sampass)) != NULL)) {
253                 memcpy(md4pw, pass, 16);
254                 dump_data(5, md4pw, 16);
255                 pdb_free_sam(&sampass);
256                 return True;
257         }
258         
259         DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
260         pdb_free_sam(&sampass);
261         return False;
262
263 }
264
265 /*************************************************************************
266  _net_req_chal
267  *************************************************************************/
268
269 NTSTATUS _net_req_chal(pipes_struct *p, NET_Q_REQ_CHAL *q_u, NET_R_REQ_CHAL *r_u)
270 {
271         NTSTATUS status = NT_STATUS_OK;
272
273         rpcstr_pull(p->dc.remote_machine,q_u->uni_logon_clnt.buffer,sizeof(fstring),q_u->uni_logon_clnt.uni_str_len*2,0);
274
275         /* create a server challenge for the client */
276         /* Set these to random values. */
277         generate_random_buffer(p->dc.srv_chal.data, 8);
278         
279         memcpy(p->dc.srv_cred.challenge.data, p->dc.srv_chal.data, 8);
280
281         memcpy(p->dc.clnt_chal.data          , q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
282         memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
283
284         memset((char *)p->dc.sess_key, '\0', sizeof(p->dc.sess_key));
285
286         p->dc.challenge_sent = True;
287         /* set up the LSA REQUEST CHALLENGE response */
288         init_net_r_req_chal(r_u, &p->dc.srv_chal, status);
289         
290         return status;
291 }
292
293 /*************************************************************************
294  init_net_r_auth:
295  *************************************************************************/
296
297 static void init_net_r_auth(NET_R_AUTH *r_a, DOM_CHAL *resp_cred, NTSTATUS status)
298 {
299         memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
300         r_a->status = status;
301 }
302
303 /*************************************************************************
304  _net_auth
305  *************************************************************************/
306
307 NTSTATUS _net_auth(pipes_struct *p, NET_Q_AUTH *q_u, NET_R_AUTH *r_u)
308 {
309         NTSTATUS status = NT_STATUS_OK;
310         DOM_CHAL srv_cred;
311         UTIME srv_time;
312         fstring mach_acct;
313
314         srv_time.time = 0;
315
316         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);
317
318         if (p->dc.challenge_sent && get_md4pw((char *)p->dc.md4pw, mach_acct)) {
319
320                 /* from client / server challenges and md4 password, generate sess key */
321                 cred_session_key(&p->dc.clnt_chal, &p->dc.srv_chal,
322                                  p->dc.md4pw, p->dc.sess_key);
323                 
324                 /* check that the client credentials are valid */
325                 if (cred_assert(&q_u->clnt_chal, p->dc.sess_key, &p->dc.clnt_cred.challenge, srv_time)) {
326                         
327                         /* create server challenge for inclusion in the reply */
328                         cred_create(p->dc.sess_key, &p->dc.srv_cred.challenge, srv_time, &srv_cred);
329                 
330                         /* copy the received client credentials for use next time */
331                         memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
332                         memcpy(p->dc.srv_cred .challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
333                         
334                         /* Save the machine account name. */
335                         fstrcpy(p->dc.mach_acct, mach_acct);
336                 
337                         p->dc.authenticated = True;
338
339                 } else {
340                         status = NT_STATUS_ACCESS_DENIED;
341                 }
342         } else {
343                 status = NT_STATUS_ACCESS_DENIED;
344         }
345         
346         /* set up the LSA AUTH response */
347         init_net_r_auth(r_u, &srv_cred, status);
348
349         return r_u->status;
350 }
351
352 /*************************************************************************
353  init_net_r_auth_2:
354  *************************************************************************/
355
356 static void init_net_r_auth_2(NET_R_AUTH_2 *r_a,
357                               DOM_CHAL *resp_cred, NEG_FLAGS *flgs, NTSTATUS status)
358 {
359         memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
360         memcpy(&r_a->srv_flgs, flgs, sizeof(r_a->srv_flgs));
361         r_a->status = status;
362 }
363
364 /*************************************************************************
365  _net_auth_2
366  *************************************************************************/
367
368 NTSTATUS _net_auth_2(pipes_struct *p, NET_Q_AUTH_2 *q_u, NET_R_AUTH_2 *r_u)
369 {
370         NTSTATUS status = NT_STATUS_OK;
371         DOM_CHAL srv_cred;
372         UTIME srv_time;
373         NEG_FLAGS srv_flgs;
374         fstring mach_acct;
375
376         srv_time.time = 0;
377
378         if ( (lp_server_schannel() == True) &&
379              ((q_u->clnt_flgs.neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
380
381                 /* schannel must be used, but client did not offer it. */
382                 status = NT_STATUS_ACCESS_DENIED;
383         }
384
385         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);
386
387         if (p->dc.challenge_sent && get_md4pw((char *)p->dc.md4pw, mach_acct)) {
388                 
389                 /* from client / server challenges and md4 password, generate sess key */
390                 cred_session_key(&p->dc.clnt_chal, &p->dc.srv_chal,
391                                  p->dc.md4pw, p->dc.sess_key);
392                 
393                 /* check that the client credentials are valid */
394                 if (cred_assert(&q_u->clnt_chal, p->dc.sess_key, &p->dc.clnt_cred.challenge, srv_time)) {
395                         
396                         /* create server challenge for inclusion in the reply */
397                         cred_create(p->dc.sess_key, &p->dc.srv_cred.challenge, srv_time, &srv_cred);
398                         
399                         /* copy the received client credentials for use next time */
400                         memcpy(p->dc.clnt_cred.challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
401                         memcpy(p->dc.srv_cred .challenge.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
402                         
403                         /* Save the machine account name. */
404                         fstrcpy(p->dc.mach_acct, mach_acct);
405                         
406                         p->dc.authenticated = True;
407
408                 } else {
409                         status = NT_STATUS_ACCESS_DENIED;
410                 }
411         } else {
412                 status = NT_STATUS_ACCESS_DENIED;
413         }
414         
415         srv_flgs.neg_flags = 0x000001ff;
416
417         if (lp_server_schannel() != False) {
418                 srv_flgs.neg_flags |= NETLOGON_NEG_SCHANNEL;
419         }
420
421         /* set up the LSA AUTH 2 response */
422         init_net_r_auth_2(r_u, &srv_cred, &srv_flgs, status);
423
424         if (NT_STATUS_IS_OK(status)) {
425                 server_auth2_negotiated = True;
426                 last_dcinfo = p->dc;
427         }
428
429         return r_u->status;
430 }
431
432 /*************************************************************************
433  _net_srv_pwset
434  *************************************************************************/
435
436 NTSTATUS _net_srv_pwset(pipes_struct *p, NET_Q_SRV_PWSET *q_u, NET_R_SRV_PWSET *r_u)
437 {
438         NTSTATUS status = NT_STATUS_ACCESS_DENIED;
439         DOM_CRED srv_cred;
440         pstring workstation;
441         SAM_ACCOUNT *sampass=NULL;
442         BOOL ret = False;
443         unsigned char pwd[16];
444         int i;
445         uint32 acct_ctrl;
446         const uchar *old_pw;
447
448         /* checks and updates credentials.  creates reply credentials */
449         if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->clnt_id.cred, &srv_cred)))
450                 return NT_STATUS_INVALID_HANDLE;
451
452         reseed_client_creds(&p->dc.clnt_cred, &q_u->clnt_id.cred);
453         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
454
455         DEBUG(5,("_net_srv_pwset: %d\n", __LINE__));
456
457         rpcstr_pull(workstation,q_u->clnt_id.login.uni_comp_name.buffer,
458                     sizeof(workstation),q_u->clnt_id.login.uni_comp_name.uni_str_len*2,0);
459
460         DEBUG(3,("Server Password Set by Wksta:[%s] on account [%s]\n", workstation, p->dc.mach_acct));
461         
462         pdb_init_sam(&sampass);
463
464         become_root();
465         ret=pdb_getsampwnam(sampass, p->dc.mach_acct);
466         unbecome_root();
467
468         /* Ensure the account exists and is a machine account. */
469         
470         acct_ctrl = pdb_get_acct_ctrl(sampass);
471
472         if (!(ret 
473               && (acct_ctrl & ACB_WSTRUST ||
474                       acct_ctrl & ACB_SVRTRUST ||
475                       acct_ctrl & ACB_DOMTRUST))) {
476                 pdb_free_sam(&sampass);
477                 return NT_STATUS_NO_SUCH_USER;
478         }
479         
480         if (pdb_get_acct_ctrl(sampass) & ACB_DISABLED) {
481                 pdb_free_sam(&sampass);
482                 return NT_STATUS_ACCOUNT_DISABLED;
483         }
484
485         cred_hash3( pwd, q_u->pwd, p->dc.sess_key, 0);
486
487         DEBUG(100,("Server password set : new given value was :\n"));
488         for(i = 0; i < sizeof(pwd); i++)
489                 DEBUG(100,("%02X ", pwd[i]));
490         DEBUG(100,("\n"));
491
492         old_pw = pdb_get_nt_passwd(sampass);
493
494         if (old_pw && memcmp(pwd, old_pw, 16) == 0) {
495                 /* Avoid backend modificiations and other fun if the 
496                    client changed the password to the *same thing* */
497
498                 ret = True;
499         } else {
500
501                 /* LM password should be NULL for machines */
502                 if (!pdb_set_lanman_passwd (sampass, NULL, PDB_CHANGED)) {
503                         pdb_free_sam(&sampass);
504                         return NT_STATUS_NO_MEMORY;
505                 }
506                 
507                 if (!pdb_set_nt_passwd     (sampass, pwd, PDB_CHANGED)) {
508                         pdb_free_sam(&sampass);
509                         return NT_STATUS_NO_MEMORY;
510                 }
511                 
512                 if (!pdb_set_pass_changed_now     (sampass)) {
513                         pdb_free_sam(&sampass);
514                         /* Not quite sure what this one qualifies as, but this will do */
515                         return NT_STATUS_UNSUCCESSFUL; 
516                 }
517                 
518                 become_root();
519                 ret = pdb_update_sam_account (sampass);
520                 unbecome_root();
521         }
522         if (ret)
523                 status = NT_STATUS_OK;
524
525         /* set up the LSA Server Password Set response */
526         init_net_r_srv_pwset(r_u, &srv_cred, status);
527
528         pdb_free_sam(&sampass);
529         return r_u->status;
530 }
531
532
533 /*************************************************************************
534  _net_sam_logoff:
535  *************************************************************************/
536
537 NTSTATUS _net_sam_logoff(pipes_struct *p, NET_Q_SAM_LOGOFF *q_u, NET_R_SAM_LOGOFF *r_u)
538 {
539         DOM_CRED srv_cred;
540
541         if (!get_valid_user_struct(p->vuid))
542                 return NT_STATUS_NO_SUCH_USER;
543
544         /* checks and updates credentials.  creates reply credentials */
545         if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, 
546                                                      &q_u->sam_id.client.cred, &srv_cred)))
547                 return NT_STATUS_INVALID_HANDLE;
548
549         /* what happens if we get a logoff for an unknown user? */
550         reseed_client_creds(&p->dc.clnt_cred, &q_u->sam_id.client.cred);
551         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
552
553         /* XXXX maybe we want to say 'no', reject the client's credentials */
554         r_u->buffer_creds = 1; /* yes, we have valid server credentials */
555         memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
556
557         r_u->status = NT_STATUS_OK;
558
559         return r_u->status;
560 }
561
562
563 /*************************************************************************
564  _net_sam_logon
565  *************************************************************************/
566
567 NTSTATUS _net_sam_logon(pipes_struct *p, NET_Q_SAM_LOGON *q_u, NET_R_SAM_LOGON *r_u)
568 {
569         NTSTATUS status = NT_STATUS_OK;
570         NET_USER_INFO_3 *usr_info = NULL;
571         NET_ID_INFO_CTR *ctr = q_u->sam_id.ctr;
572         DOM_CRED srv_cred;
573         UNISTR2 *uni_samlogon_user = NULL;
574         UNISTR2 *uni_samlogon_domain = NULL;
575         UNISTR2 *uni_samlogon_workstation = NULL;
576         fstring nt_username, nt_domain, nt_workstation;
577         auth_usersupplied_info *user_info = NULL;
578         auth_serversupplied_info *server_info = NULL;
579         SAM_ACCOUNT *sampw;
580         struct auth_context *auth_context = NULL;
581                 
582         usr_info = TALLOC_P(p->mem_ctx, NET_USER_INFO_3);
583         if (!usr_info)
584                 return NT_STATUS_NO_MEMORY;
585
586         ZERO_STRUCTP(usr_info);
587
588         /* store the user information, if there is any. */
589         r_u->user = usr_info;
590         r_u->switch_value = 0; /* indicates no info */
591         r_u->auth_resp = 1; /* authoritative response */
592         r_u->switch_value = 3; /* indicates type of validation user info */
593  
594         if (!get_valid_user_struct(p->vuid))
595                 return NT_STATUS_NO_SUCH_USER;
596
597
598         if ( (lp_server_schannel() == True) && (!p->netsec_auth_validated) ) {
599                 /* 'server schannel = yes' should enforce use of
600                    schannel, the client did offer it in auth2, but
601                    obviously did not use it. */
602                 return NT_STATUS_ACCESS_DENIED;
603         }
604
605         /* checks and updates credentials.  creates reply credentials */
606         if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->sam_id.client.cred, &srv_cred)))
607                 return NT_STATUS_INVALID_HANDLE;
608
609         r_u->buffer_creds = 1; /* yes, we have valid server credentials */
610         memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
611
612         /* find the username */
613     
614         switch (q_u->sam_id.logon_level) {
615         case INTERACTIVE_LOGON_TYPE:
616                 uni_samlogon_user = &ctr->auth.id1.uni_user_name;
617                 uni_samlogon_domain = &ctr->auth.id1.uni_domain_name;
618
619                 uni_samlogon_workstation = &ctr->auth.id1.uni_wksta_name;
620             
621                 DEBUG(3,("SAM Logon (Interactive). Domain:[%s].  ", lp_workgroup()));
622                 break;
623         case NET_LOGON_TYPE:
624                 uni_samlogon_user = &ctr->auth.id2.uni_user_name;
625                 uni_samlogon_domain = &ctr->auth.id2.uni_domain_name;
626                 uni_samlogon_workstation = &ctr->auth.id2.uni_wksta_name;
627             
628                 DEBUG(3,("SAM Logon (Network). Domain:[%s].  ", lp_workgroup()));
629                 break;
630         default:
631                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
632                 return NT_STATUS_INVALID_INFO_CLASS;
633         } /* end switch */
634
635         rpcstr_pull(nt_username,uni_samlogon_user->buffer,sizeof(nt_username),uni_samlogon_user->uni_str_len*2,0);
636         rpcstr_pull(nt_domain,uni_samlogon_domain->buffer,sizeof(nt_domain),uni_samlogon_domain->uni_str_len*2,0);
637         rpcstr_pull(nt_workstation,uni_samlogon_workstation->buffer,sizeof(nt_workstation),uni_samlogon_workstation->uni_str_len*2,0);
638
639         DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, 
640                  nt_workstation, nt_domain));
641         
642         fstrcpy(current_user_info.smb_name, nt_username);
643         sub_set_smb_name(nt_username);
644      
645         DEBUG(5,("Attempting validation level %d for unmapped username %s.\n", q_u->sam_id.ctr->switch_value, nt_username));
646
647         status = NT_STATUS_OK;
648         
649         switch (ctr->switch_value) {
650         case NET_LOGON_TYPE:
651         {
652                 const char *wksname = nt_workstation;
653                 
654                 if (!NT_STATUS_IS_OK(status = make_auth_context_fixed(&auth_context, ctr->auth.id2.lm_chal))) {
655                         return status;
656                 }
657
658                 /* For a network logon, the workstation name comes in with two
659                  * backslashes in the front. Strip them if they are there. */
660
661                 if (*wksname == '\\') wksname++;
662                 if (*wksname == '\\') wksname++;
663
664                 /* Standard challenge/response authenticaion */
665                 if (!make_user_info_netlogon_network(&user_info, 
666                                                      nt_username, nt_domain, 
667                                                      wksname,
668                                                      ctr->auth.id2.lm_chal_resp.buffer,
669                                                      ctr->auth.id2.lm_chal_resp.str_str_len,
670                                                      ctr->auth.id2.nt_chal_resp.buffer,
671                                                      ctr->auth.id2.nt_chal_resp.str_str_len)) {
672                         status = NT_STATUS_NO_MEMORY;
673                 }       
674                 break;
675         }
676         case INTERACTIVE_LOGON_TYPE:
677                 /* 'Interactive' autheticaion, supplies the password in its
678                    MD4 form, encrypted with the session key.  We will
679                    convert this to chellange/responce for the auth
680                    subsystem to chew on */
681         {
682                 const uint8 *chal;
683                 
684                 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
685                         return status;
686                 }
687                 
688                 chal = auth_context->get_ntlm_challenge(auth_context);
689
690                 if (!make_user_info_netlogon_interactive(&user_info, 
691                                                          nt_username, nt_domain, 
692                                                          nt_workstation, chal,
693                                                          ctr->auth.id1.lm_owf.data, 
694                                                          ctr->auth.id1.nt_owf.data, 
695                                                          p->dc.sess_key)) {
696                         status = NT_STATUS_NO_MEMORY;
697                 }
698                 break;
699         }
700         default:
701                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
702                 return NT_STATUS_INVALID_INFO_CLASS;
703         } /* end switch */
704         
705         if ( NT_STATUS_IS_OK(status) ) {
706                 status = auth_context->check_ntlm_password(auth_context, 
707                         user_info, &server_info);
708         }
709
710         (auth_context->free)(&auth_context);    
711         free_user_info(&user_info);
712         
713         DEBUG(5, ("_net_sam_logon: check_password returned status %s\n", 
714                   nt_errstr(status)));
715
716         /* Check account and password */
717     
718         if (!NT_STATUS_IS_OK(status)) {
719                 free_server_info(&server_info);
720                 return status;
721         }
722
723         /* moved from right after deal_with_creds above, since we weren't
724            supposed to update unless logon was successful */
725
726         reseed_client_creds(&p->dc.clnt_cred, &q_u->sam_id.client.cred);
727         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
728     
729         if (server_info->guest) {
730                 /* We don't like guest domain logons... */
731                 DEBUG(5,("_net_sam_logon: Attempted domain logon as GUEST denied.\n"));
732                 free_server_info(&server_info);
733                 return NT_STATUS_LOGON_FAILURE;
734         }
735
736         /* This is the point at which, if the login was successful, that
737            the SAM Local Security Authority should record that the user is
738            logged in to the domain.  */
739     
740         {
741                 DOM_GID *gids = NULL;
742                 const DOM_SID *user_sid = NULL;
743                 const DOM_SID *group_sid = NULL;
744                 DOM_SID domain_sid;
745                 uint32 user_rid, group_rid; 
746
747                 int num_gids = 0;
748                 pstring my_name;
749                 fstring user_sid_string;
750                 fstring group_sid_string;
751                 uchar user_session_key[16];
752                 uchar lm_session_key[16];
753                 uchar netlogon_sess_key[16];
754
755                 sampw = server_info->sam_account;
756
757                 /* set up pointer indicating user/password failed to be found */
758                 usr_info->ptr_user_info = 0;
759
760                 user_sid = pdb_get_user_sid(sampw);
761                 group_sid = pdb_get_group_sid(sampw);
762
763                 sid_copy(&domain_sid, user_sid);
764                 sid_split_rid(&domain_sid, &user_rid);
765
766                 if (!sid_peek_check_rid(&domain_sid, group_sid, &group_rid)) {
767                         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",        
768                                   pdb_get_domain(sampw), pdb_get_username(sampw),
769                                   sid_to_string(user_sid_string, user_sid),
770                                   sid_to_string(group_sid_string, group_sid)));
771                         return NT_STATUS_UNSUCCESSFUL;
772                 }
773                 
774                 
775                 if(server_info->login_server) {
776                         pstrcpy(my_name, server_info->login_server);
777                 } else {
778                         pstrcpy(my_name, global_myname());
779                 }
780                 
781                 if (!NT_STATUS_IS_OK(status 
782                                      = nt_token_to_group_list(p->mem_ctx, 
783                                                               &domain_sid, 
784                                                               server_info->ptok, 
785                                                               &num_gids, 
786                                                               &gids))) {
787                         return status;
788                 }
789
790                 ZERO_STRUCT(netlogon_sess_key);
791                 memcpy(netlogon_sess_key, p->dc.sess_key, 8);
792                 if (server_info->user_session_key.length) {
793                         memcpy(user_session_key, server_info->user_session_key.data, 
794                                MIN(sizeof(user_session_key), server_info->user_session_key.length));
795                         SamOEMhash(user_session_key, netlogon_sess_key, 16);
796                 }
797                 if (server_info->lm_session_key.length) {
798                         memcpy(lm_session_key, server_info->lm_session_key.data, 
799                                MIN(sizeof(lm_session_key), server_info->lm_session_key.length));
800                         SamOEMhash(lm_session_key, netlogon_sess_key, 16);
801                 }
802                 ZERO_STRUCT(netlogon_sess_key);
803                 
804                 init_net_user_info3(p->mem_ctx, usr_info, 
805                                     user_rid,
806                                     group_rid,   
807                                     pdb_get_username(sampw),
808                                     pdb_get_fullname(sampw),
809                                     pdb_get_homedir(sampw),
810                                     pdb_get_dir_drive(sampw),
811                                     pdb_get_logon_script(sampw),
812                                     pdb_get_profile_path(sampw),
813                                     pdb_get_logon_time(sampw),
814                                     get_time_t_max(),
815                                     get_time_t_max(),
816                                     pdb_get_pass_last_set_time(sampw),
817                                     pdb_get_pass_can_change_time(sampw),
818                                     pdb_get_pass_must_change_time(sampw),
819                                     
820                                     0, /* logon_count */
821                                     0, /* bad_pw_count */
822                                     num_gids,    /* uint32 num_groups */
823                                     gids    , /* DOM_GID *gids */
824                                     0x20    , /* uint32 user_flgs (?) */
825                                     server_info->user_session_key.length ? user_session_key : NULL,
826                                     server_info->lm_session_key.length ? lm_session_key : NULL,
827                                     my_name     , /* char *logon_srv */
828                                     pdb_get_domain(sampw),
829                                     &domain_sid,     /* DOM_SID *dom_sid */  
830                                     /* Should be users domain sid, not servers - for trusted domains */
831                                   
832                                     NULL); /* char *other_sids */
833                 ZERO_STRUCT(user_session_key);
834                 ZERO_STRUCT(lm_session_key);
835         }
836         free_server_info(&server_info);
837         return status;
838 }
839
840 /*************************************************************************
841  _ds_enum_dom_trusts
842  *************************************************************************/
843 #if 0   /* JERRY -- not correct */
844 NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
845                              DS_R_ENUM_DOM_TRUSTS *r_u)
846 {
847         NTSTATUS status = NT_STATUS_OK;
848
849         /* TODO: According to MSDN, the can only be executed against a 
850            DC or domain member running Windows 2000 or later.  Need
851            to test against a standalone 2k server and see what it 
852            does.  A windows 2000 DC includes its own domain in the 
853            list.  --jerry */
854
855         return status;
856 }
857 #endif  /* JERRY */