15827a8b55e4eec0e2eda6a8b2b83a93e4f9b6a8
[ira/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         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
453
454         DEBUG(5,("_net_srv_pwset: %d\n", __LINE__));
455
456         rpcstr_pull(workstation,q_u->clnt_id.login.uni_comp_name.buffer,
457                     sizeof(workstation),q_u->clnt_id.login.uni_comp_name.uni_str_len*2,0);
458
459         DEBUG(3,("Server Password Set by Wksta:[%s] on account [%s]\n", workstation, p->dc.mach_acct));
460         
461         pdb_init_sam(&sampass);
462
463         become_root();
464         ret=pdb_getsampwnam(sampass, p->dc.mach_acct);
465         unbecome_root();
466
467         /* Ensure the account exists and is a machine account. */
468         
469         acct_ctrl = pdb_get_acct_ctrl(sampass);
470
471         if (!(ret 
472               && (acct_ctrl & ACB_WSTRUST ||
473                       acct_ctrl & ACB_SVRTRUST ||
474                       acct_ctrl & ACB_DOMTRUST))) {
475                 pdb_free_sam(&sampass);
476                 return NT_STATUS_NO_SUCH_USER;
477         }
478         
479         if (pdb_get_acct_ctrl(sampass) & ACB_DISABLED) {
480                 pdb_free_sam(&sampass);
481                 return NT_STATUS_ACCOUNT_DISABLED;
482         }
483
484         cred_hash3( pwd, q_u->pwd, p->dc.sess_key, 0);
485
486         DEBUG(100,("Server password set : new given value was :\n"));
487         for(i = 0; i < sizeof(pwd); i++)
488                 DEBUG(100,("%02X ", pwd[i]));
489         DEBUG(100,("\n"));
490
491         old_pw = pdb_get_nt_passwd(sampass);
492
493         if (old_pw && memcmp(pwd, old_pw, 16) == 0) {
494                 /* Avoid backend modificiations and other fun if the 
495                    client changed the password to the *same thing* */
496
497                 ret = True;
498         } else {
499
500                 /* LM password should be NULL for machines */
501                 if (!pdb_set_lanman_passwd (sampass, NULL, PDB_CHANGED)) {
502                         pdb_free_sam(&sampass);
503                         return NT_STATUS_NO_MEMORY;
504                 }
505                 
506                 if (!pdb_set_nt_passwd     (sampass, pwd, PDB_CHANGED)) {
507                         pdb_free_sam(&sampass);
508                         return NT_STATUS_NO_MEMORY;
509                 }
510                 
511                 if (!pdb_set_pass_changed_now     (sampass)) {
512                         pdb_free_sam(&sampass);
513                         /* Not quite sure what this one qualifies as, but this will do */
514                         return NT_STATUS_UNSUCCESSFUL; 
515                 }
516                 
517                 become_root();
518                 ret = pdb_update_sam_account (sampass);
519                 unbecome_root();
520         }
521         if (ret)
522                 status = NT_STATUS_OK;
523
524         /* set up the LSA Server Password Set response */
525         init_net_r_srv_pwset(r_u, &srv_cred, status);
526
527         pdb_free_sam(&sampass);
528         return r_u->status;
529 }
530
531
532 /*************************************************************************
533  _net_sam_logoff:
534  *************************************************************************/
535
536 NTSTATUS _net_sam_logoff(pipes_struct *p, NET_Q_SAM_LOGOFF *q_u, NET_R_SAM_LOGOFF *r_u)
537 {
538         DOM_CRED srv_cred;
539
540         if (!get_valid_user_struct(p->vuid))
541                 return NT_STATUS_NO_SUCH_USER;
542
543         /* checks and updates credentials.  creates reply credentials */
544         if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, 
545                                                      &q_u->sam_id.client.cred, &srv_cred)))
546                 return NT_STATUS_INVALID_HANDLE;
547
548         /* what happens if we get a logoff for an unknown user? */
549         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
550
551         /* XXXX maybe we want to say 'no', reject the client's credentials */
552         r_u->buffer_creds = 1; /* yes, we have valid server credentials */
553         memcpy(&r_u->srv_creds, &srv_cred, sizeof(r_u->srv_creds));
554
555         r_u->status = NT_STATUS_OK;
556
557         return r_u->status;
558 }
559
560
561 /*************************************************************************
562  _net_sam_logon
563  *************************************************************************/
564
565 NTSTATUS _net_sam_logon(pipes_struct *p, NET_Q_SAM_LOGON *q_u, NET_R_SAM_LOGON *r_u)
566 {
567         NTSTATUS status = NT_STATUS_OK;
568         NET_USER_INFO_3 *usr_info = NULL;
569         NET_ID_INFO_CTR *ctr = q_u->sam_id.ctr;
570         DOM_CRED srv_cred;
571         UNISTR2 *uni_samlogon_user = NULL;
572         UNISTR2 *uni_samlogon_domain = NULL;
573         UNISTR2 *uni_samlogon_workstation = NULL;
574         fstring nt_username, nt_domain, nt_workstation;
575         auth_usersupplied_info *user_info = NULL;
576         auth_serversupplied_info *server_info = NULL;
577         SAM_ACCOUNT *sampw;
578         struct auth_context *auth_context = NULL;
579                 
580         usr_info = TALLOC_P(p->mem_ctx, NET_USER_INFO_3);
581         if (!usr_info)
582                 return NT_STATUS_NO_MEMORY;
583
584         ZERO_STRUCTP(usr_info);
585
586         /* store the user information, if there is any. */
587         r_u->user = usr_info;
588         r_u->switch_value = 0; /* indicates no info */
589         r_u->auth_resp = 1; /* authoritative response */
590         r_u->switch_value = 3; /* indicates type of validation user info */
591  
592         if (!get_valid_user_struct(p->vuid))
593                 return NT_STATUS_NO_SUCH_USER;
594
595
596         if ( (lp_server_schannel() == True) && (!p->netsec_auth_validated) ) {
597                 /* 'server schannel = yes' should enforce use of
598                    schannel, the client did offer it in auth2, but
599                    obviously did not use it. */
600                 return NT_STATUS_ACCESS_DENIED;
601         }
602
603         /* checks and updates credentials.  creates reply credentials */
604         if (!(p->dc.authenticated && deal_with_creds(p->dc.sess_key, &p->dc.clnt_cred, &q_u->sam_id.client.cred, &srv_cred)))
605                 return NT_STATUS_INVALID_HANDLE;
606
607         memcpy(&p->dc.srv_cred, &p->dc.clnt_cred, sizeof(p->dc.clnt_cred));
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                 /* If we don't know what this domain is, we need to 
720                    indicate that we are not authoritative.  This 
721                    allows the client to decide if it needs to try 
722                    a local user.  Fix by jpjanosi@us.ibm.com, #2976 */
723                 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) 
724                      && !strequal(nt_domain, get_global_sam_name())
725                      && !is_trusted_domain(nt_domain) )
726                         r_u->auth_resp = 0; /* We are not authoritative */
727
728                 free_server_info(&server_info);
729                 return status;
730         }
731
732         if (server_info->guest) {
733                 /* We don't like guest domain logons... */
734                 DEBUG(5,("_net_sam_logon: Attempted domain logon as GUEST denied.\n"));
735                 free_server_info(&server_info);
736                 return NT_STATUS_LOGON_FAILURE;
737         }
738
739         /* This is the point at which, if the login was successful, that
740            the SAM Local Security Authority should record that the user is
741            logged in to the domain.  */
742     
743         {
744                 DOM_GID *gids = NULL;
745                 const DOM_SID *user_sid = NULL;
746                 const DOM_SID *group_sid = NULL;
747                 DOM_SID domain_sid;
748                 uint32 user_rid, group_rid; 
749
750                 int num_gids = 0;
751                 pstring my_name;
752                 fstring user_sid_string;
753                 fstring group_sid_string;
754                 uchar user_session_key[16];
755                 uchar lm_session_key[16];
756                 uchar netlogon_sess_key[16];
757
758                 sampw = server_info->sam_account;
759
760                 /* set up pointer indicating user/password failed to be found */
761                 usr_info->ptr_user_info = 0;
762
763                 user_sid = pdb_get_user_sid(sampw);
764                 group_sid = pdb_get_group_sid(sampw);
765
766                 sid_copy(&domain_sid, user_sid);
767                 sid_split_rid(&domain_sid, &user_rid);
768
769                 if (!sid_peek_check_rid(&domain_sid, group_sid, &group_rid)) {
770                         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",        
771                                   pdb_get_domain(sampw), pdb_get_username(sampw),
772                                   sid_to_string(user_sid_string, user_sid),
773                                   sid_to_string(group_sid_string, group_sid)));
774                         return NT_STATUS_UNSUCCESSFUL;
775                 }
776                 
777                 
778                 if(server_info->login_server) {
779                         pstrcpy(my_name, server_info->login_server);
780                 } else {
781                         pstrcpy(my_name, global_myname());
782                 }
783                 
784                 if (!NT_STATUS_IS_OK(status 
785                                      = nt_token_to_group_list(p->mem_ctx, 
786                                                               &domain_sid, 
787                                                               server_info->ptok, 
788                                                               &num_gids, 
789                                                               &gids))) {
790                         return status;
791                 }
792
793                 ZERO_STRUCT(netlogon_sess_key);
794                 memcpy(netlogon_sess_key, p->dc.sess_key, 8);
795                 if (server_info->user_session_key.length) {
796                         memcpy(user_session_key, server_info->user_session_key.data, 
797                                MIN(sizeof(user_session_key), server_info->user_session_key.length));
798                         SamOEMhash(user_session_key, netlogon_sess_key, 16);
799                 }
800                 if (server_info->lm_session_key.length) {
801                         memcpy(lm_session_key, server_info->lm_session_key.data, 
802                                MIN(sizeof(lm_session_key), server_info->lm_session_key.length));
803                         SamOEMhash(lm_session_key, netlogon_sess_key, 16);
804                 }
805                 ZERO_STRUCT(netlogon_sess_key);
806                 
807                 init_net_user_info3(p->mem_ctx, usr_info, 
808                                     user_rid,
809                                     group_rid,   
810                                     pdb_get_username(sampw),
811                                     pdb_get_fullname(sampw),
812                                     pdb_get_homedir(sampw),
813                                     pdb_get_dir_drive(sampw),
814                                     pdb_get_logon_script(sampw),
815                                     pdb_get_profile_path(sampw),
816                                     pdb_get_logon_time(sampw),
817                                     get_time_t_max(),
818                                     get_time_t_max(),
819                                     pdb_get_pass_last_set_time(sampw),
820                                     pdb_get_pass_can_change_time(sampw),
821                                     pdb_get_pass_must_change_time(sampw),
822                                     
823                                     0, /* logon_count */
824                                     0, /* bad_pw_count */
825                                     num_gids,    /* uint32 num_groups */
826                                     gids    , /* DOM_GID *gids */
827                                     0x20    , /* uint32 user_flgs (?) */
828                                     server_info->user_session_key.length ? user_session_key : NULL,
829                                     server_info->lm_session_key.length ? lm_session_key : NULL,
830                                     my_name     , /* char *logon_srv */
831                                     pdb_get_domain(sampw),
832                                     &domain_sid,     /* DOM_SID *dom_sid */  
833                                     /* Should be users domain sid, not servers - for trusted domains */
834                                   
835                                     NULL); /* char *other_sids */
836                 ZERO_STRUCT(user_session_key);
837                 ZERO_STRUCT(lm_session_key);
838         }
839         free_server_info(&server_info);
840         return status;
841 }
842
843 /*************************************************************************
844  _ds_enum_dom_trusts
845  *************************************************************************/
846 #if 0   /* JERRY -- not correct */
847 NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
848                              DS_R_ENUM_DOM_TRUSTS *r_u)
849 {
850         NTSTATUS status = NT_STATUS_OK;
851
852         /* TODO: According to MSDN, the can only be executed against a 
853            DC or domain member running Windows 2000 or later.  Need
854            to test against a standalone 2k server and see what it 
855            does.  A windows 2000 DC includes its own domain in the 
856            list.  --jerry */
857
858         return status;
859 }
860 #endif  /* JERRY */