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