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