r23055: Rewrite messages.c to use auto-generated marshalling in the tdb. I'm
[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 userdom_struct current_user_info;
30
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_RPC_SRV
33
34 /*************************************************************************
35  init_net_r_req_chal:
36  *************************************************************************/
37
38 static void init_net_r_req_chal(NET_R_REQ_CHAL *r_c,
39                                 DOM_CHAL *srv_chal, NTSTATUS status)
40 {
41         DEBUG(6,("init_net_r_req_chal: %d\n", __LINE__));
42         memcpy(r_c->srv_chal.data, srv_chal->data, sizeof(srv_chal->data));
43         r_c->status = status;
44 }
45
46 /*************************************************************************
47  error messages cropping up when using nltest.exe...
48  *************************************************************************/
49
50 #define ERROR_NO_SUCH_DOMAIN   0x54b
51 #define ERROR_NO_LOGON_SERVERS 0x51f
52 #define NO_ERROR               0x0
53
54 /*************************************************************************
55  net_reply_logon_ctrl:
56  *************************************************************************/
57
58 NTSTATUS _net_logon_ctrl(pipes_struct *p, NET_Q_LOGON_CTRL *q_u, 
59                        NET_R_LOGON_CTRL *r_u)
60 {
61         uint32 flags = 0x0;
62         uint32 pdc_connection_status = 0x00; /* Maybe a win32 error code? */
63         
64         /* Setup the Logon Control response */
65
66         init_net_r_logon_ctrl(r_u, q_u->query_level, flags, 
67                               pdc_connection_status);
68
69         return r_u->status;
70 }
71
72 /****************************************************************************
73 Send a message to smbd to do a sam synchronisation
74 **************************************************************************/
75
76 static void send_sync_message(void)
77 {
78         DEBUG(3, ("sending sam synchronisation message\n"));
79         message_send_all(smbd_messaging_context(), MSG_SMB_SAM_SYNC, NULL, 0,
80                          NULL);
81 }
82
83 /*************************************************************************
84  net_reply_logon_ctrl2:
85  *************************************************************************/
86
87 NTSTATUS _net_logon_ctrl2(pipes_struct *p, NET_Q_LOGON_CTRL2 *q_u, NET_R_LOGON_CTRL2 *r_u)
88 {
89         uint32 flags = 0x0;
90         uint32 pdc_connection_status = 0x0;
91         uint32 logon_attempts = 0x0;
92         uint32 tc_status;
93         fstring servername, domain, dc_name, dc_name2;
94         struct in_addr dc_ip;
95
96         /* this should be \\global_myname() */
97         unistr2_to_ascii(servername, &q_u->uni_server_name, sizeof(servername));
98
99         r_u->status = NT_STATUS_OK;
100         
101         tc_status = ERROR_NO_SUCH_DOMAIN;
102         fstrcpy( dc_name, "" );
103         
104         switch ( q_u->function_code ) {
105                 case NETLOGON_CONTROL_TC_QUERY:
106                         unistr2_to_ascii(domain, &q_u->info.info6.domain, sizeof(domain));
107                                 
108                         if ( !is_trusted_domain( domain ) )
109                                 break;
110                                 
111                         if ( !get_dc_name( domain, NULL, dc_name2, &dc_ip ) ) {
112                                 tc_status = ERROR_NO_LOGON_SERVERS;
113                                 break;
114                         }
115
116                         fstr_sprintf( dc_name, "\\\\%s", dc_name2 );
117                                 
118                         tc_status = NO_ERROR;
119                         
120                         break;
121                         
122                 case NETLOGON_CONTROL_REDISCOVER:
123                         unistr2_to_ascii(domain, &q_u->info.info6.domain, sizeof(domain));
124                                 
125                         if ( !is_trusted_domain( domain ) )
126                                 break;
127                                 
128                         if ( !get_dc_name( domain, NULL, dc_name2, &dc_ip ) ) {
129                                 tc_status = ERROR_NO_LOGON_SERVERS;
130                                 break;
131                         }
132
133                         fstr_sprintf( dc_name, "\\\\%s", dc_name2 );
134                                 
135                         tc_status = NO_ERROR;
136                         
137                         break;
138                         
139                 default:
140                         /* no idea what this should be */
141                         DEBUG(0,("_net_logon_ctrl2: unimplemented function level [%d]\n",
142                                 q_u->function_code));
143         }
144         
145         /* prepare the response */
146         
147         init_net_r_logon_ctrl2( r_u, q_u->query_level, flags, 
148                 pdc_connection_status, logon_attempts, tc_status, dc_name );
149
150         if (lp_server_role() == ROLE_DOMAIN_BDC)
151                 send_sync_message();
152
153         return r_u->status;
154 }
155
156 /*************************************************************************
157  net_reply_trust_dom_list:
158  *************************************************************************/
159
160 NTSTATUS _net_trust_dom_list(pipes_struct *p, NET_Q_TRUST_DOM_LIST *q_u, NET_R_TRUST_DOM_LIST *r_u)
161 {
162         const char *trusted_domain = "test_domain";
163         uint32 num_trust_domains = 1;
164
165         DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
166
167         /* set up the Trusted Domain List response */
168         init_r_trust_dom(r_u, num_trust_domains, trusted_domain);
169
170         DEBUG(6,("_net_trust_dom_list: %d\n", __LINE__));
171
172         return r_u->status;
173 }
174
175 /***********************************************************************************
176  init_net_r_srv_pwset:
177  ***********************************************************************************/
178
179 static void init_net_r_srv_pwset(NET_R_SRV_PWSET *r_s,
180                                  DOM_CRED *srv_cred, NTSTATUS status)  
181 {
182         DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
183
184         memcpy(&r_s->srv_cred, srv_cred, sizeof(r_s->srv_cred));
185         r_s->status = status;
186
187         DEBUG(5,("init_net_r_srv_pwset: %d\n", __LINE__));
188 }
189
190 /******************************************************************
191  gets a machine password entry.  checks access rights of the host.
192  ******************************************************************/
193
194 static NTSTATUS get_md4pw(char *md4pw, char *mach_acct, uint16 sec_chan_type)
195 {
196         struct samu *sampass = NULL;
197         const uint8 *pass;
198         BOOL ret;
199         uint32 acct_ctrl;
200
201 #if 0
202     /*
203      * Currently this code is redundent as we already have a filter
204      * by hostname list. What this code really needs to do is to 
205      * get a hosts allowed/hosts denied list from the SAM database
206      * on a per user basis, and make the access decision there.
207      * I will leave this code here for now as a reminder to implement
208      * this at a later date. JRA.
209      */
210
211         if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
212                           client_name(), client_addr()))
213         {
214                 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
215                 return False;
216         }
217 #endif /* 0 */
218
219         if ( !(sampass = samu_new( NULL )) ) {
220                 return NT_STATUS_NO_MEMORY;
221         }
222
223         /* JRA. This is ok as it is only used for generating the challenge. */
224         become_root();
225         ret = pdb_getsampwnam(sampass, mach_acct);
226         unbecome_root();
227  
228         if (!ret) {
229                 DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
230                 TALLOC_FREE(sampass);
231                 return NT_STATUS_ACCESS_DENIED;
232         }
233
234         acct_ctrl = pdb_get_acct_ctrl(sampass);
235         if (acct_ctrl & ACB_DISABLED) {
236                 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
237                 TALLOC_FREE(sampass);
238                 return NT_STATUS_ACCOUNT_DISABLED;
239         }
240
241         if (!(acct_ctrl & ACB_SVRTRUST) &&
242             !(acct_ctrl & ACB_WSTRUST) &&
243             !(acct_ctrl & ACB_DOMTRUST)) 
244         {
245                 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
246                 TALLOC_FREE(sampass);
247                 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
248         }
249
250         switch (sec_chan_type) {
251                 case SEC_CHAN_BDC:
252                         if (!(acct_ctrl & ACB_SVRTRUST)) {
253                                 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
254                                          "but not a server trust account\n", mach_acct));
255                                 TALLOC_FREE(sampass);
256                                 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
257                         }
258                         break;
259                 case SEC_CHAN_WKSTA:
260                         if (!(acct_ctrl & ACB_WSTRUST)) {
261                                 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
262                                          "but not a workstation trust account\n", mach_acct));
263                                 TALLOC_FREE(sampass);
264                                 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
265                         }
266                         break;
267                 case SEC_CHAN_DOMAIN:
268                         if (!(acct_ctrl & ACB_DOMTRUST)) {
269                                 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
270                                          "but not a interdomain trust account\n", mach_acct));
271                                 TALLOC_FREE(sampass);
272                                 return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
273                         }
274                         break;
275                 default:
276                         break;
277         }
278
279         if ((pass = pdb_get_nt_passwd(sampass)) == NULL) {
280                 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
281                 TALLOC_FREE(sampass);
282                 return NT_STATUS_LOGON_FAILURE;
283         }
284
285         memcpy(md4pw, pass, 16);
286         dump_data(5, (uint8 *)md4pw, 16);
287
288         TALLOC_FREE(sampass);
289         
290         return NT_STATUS_OK;
291         
292
293 }
294
295 /*************************************************************************
296  _net_req_chal
297  *************************************************************************/
298
299 NTSTATUS _net_req_chal(pipes_struct *p, NET_Q_REQ_CHAL *q_u, NET_R_REQ_CHAL *r_u)
300 {
301         if (!p->dc) {
302                 p->dc = TALLOC_ZERO_P(p->pipe_state_mem_ctx, struct dcinfo);
303                 if (!p->dc) {
304                         return NT_STATUS_NO_MEMORY;
305                 }
306         } else {
307                 DEBUG(10,("_net_req_chal: new challenge requested. Clearing old state.\n"));
308                 ZERO_STRUCTP(p->dc);
309         }
310
311         rpcstr_pull(p->dc->remote_machine,
312                         q_u->uni_logon_clnt.buffer,
313                         sizeof(fstring),q_u->uni_logon_clnt.uni_str_len*2,0);
314
315         /* Save the client challenge to the server. */
316         memcpy(p->dc->clnt_chal.data, q_u->clnt_chal.data, sizeof(q_u->clnt_chal.data));
317
318         /* Create a server challenge for the client */
319         /* Set this to a random value. */
320         generate_random_buffer(p->dc->srv_chal.data, 8);
321         
322         /* set up the LSA REQUEST CHALLENGE response */
323         init_net_r_req_chal(r_u, &p->dc->srv_chal, NT_STATUS_OK);
324         
325         p->dc->challenge_sent = True;
326
327         return NT_STATUS_OK;
328 }
329
330 /*************************************************************************
331  init_net_r_auth:
332  *************************************************************************/
333
334 static void init_net_r_auth(NET_R_AUTH *r_a, DOM_CHAL *resp_cred, NTSTATUS status)
335 {
336         memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
337         r_a->status = status;
338 }
339
340 /*************************************************************************
341  _net_auth. Create the initial credentials.
342  *************************************************************************/
343
344 NTSTATUS _net_auth(pipes_struct *p, NET_Q_AUTH *q_u, NET_R_AUTH *r_u)
345 {
346         NTSTATUS status;
347         fstring mach_acct;
348         fstring remote_machine;
349         DOM_CHAL srv_chal_out;
350
351         if (!p->dc || !p->dc->challenge_sent) {
352                 return NT_STATUS_ACCESS_DENIED;
353         }
354
355         rpcstr_pull(mach_acct, q_u->clnt_id.uni_acct_name.buffer,sizeof(fstring),
356                                 q_u->clnt_id.uni_acct_name.uni_str_len*2,0);
357         rpcstr_pull(remote_machine, q_u->clnt_id.uni_comp_name.buffer,sizeof(fstring),
358                                 q_u->clnt_id.uni_comp_name.uni_str_len*2,0);
359
360         status = get_md4pw((char *)p->dc->mach_pw, mach_acct, q_u->clnt_id.sec_chan);
361         if (!NT_STATUS_IS_OK(status)) {
362                 DEBUG(0,("_net_auth: creds_server_check failed. Failed to "
363                         "get password for machine account %s "
364                         "from client %s: %s\n",
365                         mach_acct, remote_machine, nt_errstr(status) ));
366                 /* always return NT_STATUS_ACCESS_DENIED */
367                 return NT_STATUS_ACCESS_DENIED;
368         }
369
370         /* From the client / server challenges and md4 password, generate sess key */
371         creds_server_init(0,                    /* No neg flags. */
372                         p->dc,
373                         &p->dc->clnt_chal,      /* Stored client chal. */
374                         &p->dc->srv_chal,       /* Stored server chal. */
375                         p->dc->mach_pw,
376                         &srv_chal_out); 
377
378         /* Check client credentials are valid. */
379         if (!creds_server_check(p->dc, &q_u->clnt_chal)) {
380                 DEBUG(0,("_net_auth: creds_server_check failed. Rejecting auth "
381                         "request from client %s machine account %s\n",
382                         remote_machine, mach_acct ));
383                 return NT_STATUS_ACCESS_DENIED;
384         }
385
386         fstrcpy(p->dc->mach_acct, mach_acct);
387         fstrcpy(p->dc->remote_machine, remote_machine);
388         p->dc->authenticated = True;
389
390         /* set up the LSA AUTH response */
391         /* Return the server credentials. */
392         init_net_r_auth(r_u, &srv_chal_out, NT_STATUS_OK);
393
394         return r_u->status;
395 }
396
397 /*************************************************************************
398  init_net_r_auth_2:
399  *************************************************************************/
400
401 static void init_net_r_auth_2(NET_R_AUTH_2 *r_a,
402                               DOM_CHAL *resp_cred, NEG_FLAGS *flgs, NTSTATUS status)
403 {
404         memcpy(r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
405         memcpy(&r_a->srv_flgs, flgs, sizeof(r_a->srv_flgs));
406         r_a->status = status;
407 }
408
409 /*************************************************************************
410  _net_auth_2
411  *************************************************************************/
412
413 NTSTATUS _net_auth_2(pipes_struct *p, NET_Q_AUTH_2 *q_u, NET_R_AUTH_2 *r_u)
414 {
415         NTSTATUS status;
416         NEG_FLAGS srv_flgs;
417         fstring mach_acct;
418         fstring remote_machine;
419         DOM_CHAL srv_chal_out;
420
421         rpcstr_pull(mach_acct, q_u->clnt_id.uni_acct_name.buffer,sizeof(fstring),
422                                 q_u->clnt_id.uni_acct_name.uni_str_len*2,0);
423
424         /* We use this as the key to store the creds. */
425         rpcstr_pull(remote_machine, q_u->clnt_id.uni_comp_name.buffer,sizeof(fstring),
426                                 q_u->clnt_id.uni_comp_name.uni_str_len*2,0);
427
428         if (!p->dc || !p->dc->challenge_sent) {
429                 DEBUG(0,("_net_auth2: no challenge sent to client %s\n",
430                         remote_machine ));
431                 return NT_STATUS_ACCESS_DENIED;
432         }
433
434         if ( (lp_server_schannel() == True) &&
435              ((q_u->clnt_flgs.neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
436
437                 /* schannel must be used, but client did not offer it. */
438                 DEBUG(0,("_net_auth2: schannel required but client failed "
439                         "to offer it. Client was %s\n",
440                         mach_acct ));
441                 return NT_STATUS_ACCESS_DENIED;
442         }
443
444         status = get_md4pw((char *)p->dc->mach_pw, mach_acct, q_u->clnt_id.sec_chan);
445         if (!NT_STATUS_IS_OK(status)) {
446                 DEBUG(0,("_net_auth2: failed to get machine password for "
447                         "account %s: %s\n",
448                         mach_acct, nt_errstr(status) ));
449                 /* always return NT_STATUS_ACCESS_DENIED */
450                 return NT_STATUS_ACCESS_DENIED;
451         }
452
453         /* From the client / server challenges and md4 password, generate sess key */
454         creds_server_init(q_u->clnt_flgs.neg_flags,
455                         p->dc,
456                         &p->dc->clnt_chal,      /* Stored client chal. */
457                         &p->dc->srv_chal,       /* Stored server chal. */
458                         p->dc->mach_pw,
459                         &srv_chal_out); 
460
461         /* Check client credentials are valid. */
462         if (!creds_server_check(p->dc, &q_u->clnt_chal)) {
463                 DEBUG(0,("_net_auth2: creds_server_check failed. Rejecting auth "
464                         "request from client %s machine account %s\n",
465                         remote_machine, mach_acct ));
466                 return NT_STATUS_ACCESS_DENIED;
467         }
468
469         srv_flgs.neg_flags = 0x000001ff;
470
471         if (lp_server_schannel() != False) {
472                 srv_flgs.neg_flags |= NETLOGON_NEG_SCHANNEL;
473         }
474
475         /* set up the LSA AUTH 2 response */
476         init_net_r_auth_2(r_u, &srv_chal_out, &srv_flgs, NT_STATUS_OK);
477
478         fstrcpy(p->dc->mach_acct, mach_acct);
479         fstrcpy(p->dc->remote_machine, remote_machine);
480         fstrcpy(p->dc->domain, lp_workgroup() );
481
482         p->dc->authenticated = True;
483
484         /* Store off the state so we can continue after client disconnect. */
485         become_root();
486         secrets_store_schannel_session_info(p->mem_ctx,
487                                         remote_machine,
488                                         p->dc);
489         unbecome_root();
490
491         return r_u->status;
492 }
493
494 /*************************************************************************
495  _net_srv_pwset
496  *************************************************************************/
497
498 NTSTATUS _net_srv_pwset(pipes_struct *p, NET_Q_SRV_PWSET *q_u, NET_R_SRV_PWSET *r_u)
499 {
500         fstring remote_machine;
501         struct samu *sampass=NULL;
502         BOOL ret = False;
503         unsigned char pwd[16];
504         int i;
505         uint32 acct_ctrl;
506         DOM_CRED cred_out;
507         const uchar *old_pw;
508
509         DEBUG(5,("_net_srv_pwset: %d\n", __LINE__));
510
511         /* We need the remote machine name for the creds lookup. */
512         rpcstr_pull(remote_machine,q_u->clnt_id.login.uni_comp_name.buffer,
513                     sizeof(remote_machine),q_u->clnt_id.login.uni_comp_name.uni_str_len*2,0);
514
515         if ( (lp_server_schannel() == True) && (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) ) {
516                 /* 'server schannel = yes' should enforce use of
517                    schannel, the client did offer it in auth2, but
518                    obviously did not use it. */
519                 DEBUG(0,("_net_srv_pwset: client %s not using schannel for netlogon\n",
520                         remote_machine ));
521                 return NT_STATUS_ACCESS_DENIED;
522         }
523
524         if (!p->dc) {
525                 /* Restore the saved state of the netlogon creds. */
526                 become_root();
527                 ret = secrets_restore_schannel_session_info(p->pipe_state_mem_ctx,
528                                                         remote_machine,
529                                                         &p->dc);
530                 unbecome_root();
531                 if (!ret) {
532                         return NT_STATUS_INVALID_HANDLE;
533                 }
534         }
535
536         if (!p->dc || !p->dc->authenticated) {
537                 return NT_STATUS_INVALID_HANDLE;
538         }
539
540         DEBUG(3,("_net_srv_pwset: Server Password Set by remote machine:[%s] on account [%s]\n",
541                         remote_machine, p->dc->mach_acct));
542         
543         /* Step the creds chain forward. */
544         if (!creds_server_step(p->dc, &q_u->clnt_id.cred, &cred_out)) {
545                 DEBUG(2,("_net_srv_pwset: creds_server_step failed. Rejecting auth "
546                         "request from client %s machine account %s\n",
547                         remote_machine, p->dc->mach_acct ));
548                 return NT_STATUS_INVALID_PARAMETER;
549         }
550
551         /* We must store the creds state after an update. */
552         sampass = samu_new( NULL );
553         if (!sampass) {
554                 return NT_STATUS_NO_MEMORY;
555         }
556
557         become_root();
558         secrets_store_schannel_session_info(p->pipe_state_mem_ctx,
559                                                 remote_machine,
560                                                 p->dc);
561         ret = pdb_getsampwnam(sampass, p->dc->mach_acct);
562         unbecome_root();
563
564         if (!ret) {
565                 TALLOC_FREE(sampass);
566                 return NT_STATUS_ACCESS_DENIED;
567         }
568
569         /* Ensure the account exists and is a machine account. */
570         
571         acct_ctrl = pdb_get_acct_ctrl(sampass);
572
573         if (!(acct_ctrl & ACB_WSTRUST ||
574                       acct_ctrl & ACB_SVRTRUST ||
575                       acct_ctrl & ACB_DOMTRUST)) {
576                 TALLOC_FREE(sampass);
577                 return NT_STATUS_NO_SUCH_USER;
578         }
579         
580         if (pdb_get_acct_ctrl(sampass) & ACB_DISABLED) {
581                 TALLOC_FREE(sampass);
582                 return NT_STATUS_ACCOUNT_DISABLED;
583         }
584
585         /* Woah - what does this to to the credential chain ? JRA */
586         cred_hash3( pwd, q_u->pwd, p->dc->sess_key, 0);
587
588         DEBUG(100,("Server password set : new given value was :\n"));
589         for(i = 0; i < sizeof(pwd); i++)
590                 DEBUG(100,("%02X ", pwd[i]));
591         DEBUG(100,("\n"));
592
593         old_pw = pdb_get_nt_passwd(sampass);
594
595         if (old_pw && memcmp(pwd, old_pw, 16) == 0) {
596                 /* Avoid backend modificiations and other fun if the 
597                    client changed the password to the *same thing* */
598
599                 ret = True;
600         } else {
601
602                 /* LM password should be NULL for machines */
603                 if (!pdb_set_lanman_passwd(sampass, NULL, PDB_CHANGED)) {
604                         TALLOC_FREE(sampass);
605                         return NT_STATUS_NO_MEMORY;
606                 }
607                 
608                 if (!pdb_set_nt_passwd(sampass, pwd, PDB_CHANGED)) {
609                         TALLOC_FREE(sampass);
610                         return NT_STATUS_NO_MEMORY;
611                 }
612                 
613                 if (!pdb_set_pass_last_set_time(sampass, time(NULL), PDB_CHANGED)) {
614                         TALLOC_FREE(sampass);
615                         /* Not quite sure what this one qualifies as, but this will do */
616                         return NT_STATUS_UNSUCCESSFUL; 
617                 }
618                 
619                 become_root();
620                 r_u->status = pdb_update_sam_account(sampass);
621                 unbecome_root();
622         }
623
624         /* set up the LSA Server Password Set response */
625         init_net_r_srv_pwset(r_u, &cred_out, r_u->status);
626
627         TALLOC_FREE(sampass);
628         return r_u->status;
629 }
630
631 /*************************************************************************
632  _net_sam_logoff:
633  *************************************************************************/
634
635 NTSTATUS _net_sam_logoff(pipes_struct *p, NET_Q_SAM_LOGOFF *q_u, NET_R_SAM_LOGOFF *r_u)
636 {
637         fstring remote_machine;
638
639         if ( (lp_server_schannel() == True) && (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) ) {
640                 /* 'server schannel = yes' should enforce use of
641                    schannel, the client did offer it in auth2, but
642                    obviously did not use it. */
643                 DEBUG(0,("_net_sam_logoff: client %s not using schannel for netlogon\n",
644                         get_remote_machine_name() ));
645                 return NT_STATUS_ACCESS_DENIED;
646         }
647
648
649         if (!get_valid_user_struct(p->vuid))
650                 return NT_STATUS_NO_SUCH_USER;
651
652         /* Get the remote machine name for the creds store. */
653         rpcstr_pull(remote_machine,q_u->sam_id.client.login.uni_comp_name.buffer,
654                     sizeof(remote_machine),q_u->sam_id.client.login.uni_comp_name.uni_str_len*2,0);
655
656         if (!p->dc) {
657                 /* Restore the saved state of the netlogon creds. */
658                 BOOL ret;
659
660                 become_root();
661                 ret = secrets_restore_schannel_session_info(p->pipe_state_mem_ctx,
662                                                 remote_machine,
663                                                 &p->dc);
664                 unbecome_root();
665                 if (!ret) {
666                         return NT_STATUS_INVALID_HANDLE;
667                 }
668         }
669
670         if (!p->dc || !p->dc->authenticated) {
671                 return NT_STATUS_INVALID_HANDLE;
672         }
673
674         r_u->buffer_creds = 1; /* yes, we have valid server credentials */
675
676         /* checks and updates credentials.  creates reply credentials */
677         if (!creds_server_step(p->dc, &q_u->sam_id.client.cred, &r_u->srv_creds)) {
678                 DEBUG(2,("_net_sam_logoff: creds_server_step failed. Rejecting auth "
679                         "request from client %s machine account %s\n",
680                         remote_machine, p->dc->mach_acct ));
681                 return NT_STATUS_INVALID_PARAMETER;
682         }
683
684         /* We must store the creds state after an update. */
685         become_root();
686         secrets_store_schannel_session_info(p->pipe_state_mem_ctx,
687                                         remote_machine,
688                                         p->dc);
689         unbecome_root();
690
691         r_u->status = NT_STATUS_OK;
692         return r_u->status;
693 }
694
695 /*******************************************************************
696  gets a domain user's groups from their already-calculated NT_USER_TOKEN
697  ********************************************************************/
698
699 static NTSTATUS nt_token_to_group_list(TALLOC_CTX *mem_ctx,
700                                        const DOM_SID *domain_sid,
701                                        size_t num_sids,
702                                        const DOM_SID *sids,
703                                        int *numgroups, DOM_GID **pgids) 
704 {
705         int i;
706
707         *numgroups=0;
708         *pgids = NULL;
709
710         for (i=0; i<num_sids; i++) {
711                 DOM_GID gid;
712                 if (!sid_peek_check_rid(domain_sid, &sids[i], &gid.g_rid)) {
713                         continue;
714                 }
715                 gid.attr = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
716                             SE_GROUP_ENABLED);
717                 ADD_TO_ARRAY(mem_ctx, DOM_GID, gid, pgids, numgroups);
718                 if (*pgids == NULL) {
719                         return NT_STATUS_NO_MEMORY;
720                 }
721         }
722         return NT_STATUS_OK;
723 }
724
725 /*************************************************************************
726  _net_sam_logon
727  *************************************************************************/
728
729 static NTSTATUS _net_sam_logon_internal(pipes_struct *p,
730                                         NET_Q_SAM_LOGON *q_u,
731                                         NET_R_SAM_LOGON *r_u,
732                                         BOOL process_creds)
733 {
734         NTSTATUS status = NT_STATUS_OK;
735         NET_USER_INFO_3 *usr_info = NULL;
736         NET_ID_INFO_CTR *ctr = q_u->sam_id.ctr;
737         UNISTR2 *uni_samlogon_user = NULL;
738         UNISTR2 *uni_samlogon_domain = NULL;
739         UNISTR2 *uni_samlogon_workstation = NULL;
740         fstring nt_username, nt_domain, nt_workstation;
741         auth_usersupplied_info *user_info = NULL;
742         auth_serversupplied_info *server_info = NULL;
743         struct samu *sampw;
744         struct auth_context *auth_context = NULL;
745          
746         if ( (lp_server_schannel() == True) && (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) ) {
747                 /* 'server schannel = yes' should enforce use of
748                    schannel, the client did offer it in auth2, but
749                    obviously did not use it. */
750                 DEBUG(0,("_net_sam_logon_internal: client %s not using schannel for netlogon\n",
751                         get_remote_machine_name() ));
752                 return NT_STATUS_ACCESS_DENIED;
753         }
754
755         usr_info = TALLOC_P(p->mem_ctx, NET_USER_INFO_3);
756         if (!usr_info) {
757                 return NT_STATUS_NO_MEMORY;
758         }
759
760         ZERO_STRUCTP(usr_info);
761
762         /* store the user information, if there is any. */
763         r_u->user = usr_info;
764         r_u->auth_resp = 1; /* authoritative response */
765         if (q_u->validation_level != 2 && q_u->validation_level != 3) {
766                 DEBUG(0,("_net_sam_logon: bad validation_level value %d.\n", (int)q_u->validation_level ));
767                 return NT_STATUS_ACCESS_DENIED;
768         }
769         /* We handle the return of USER_INFO_2 instead of 3 in the parse return. Sucks, I know... */
770         r_u->switch_value = q_u->validation_level; /* indicates type of validation user info */
771         r_u->buffer_creds = 1; /* Ensure we always return server creds. */
772  
773         if (!get_valid_user_struct(p->vuid))
774                 return NT_STATUS_NO_SUCH_USER;
775
776         if (process_creds) {
777                 fstring remote_machine;
778
779                 /* Get the remote machine name for the creds store. */
780                 /* Note this is the remote machine this request is coming from (member server),
781                    not neccessarily the workstation name the user is logging onto.
782                 */
783                 rpcstr_pull(remote_machine,q_u->sam_id.client.login.uni_comp_name.buffer,
784                     sizeof(remote_machine),q_u->sam_id.client.login.uni_comp_name.uni_str_len*2,0);
785
786                 if (!p->dc) {
787                         /* Restore the saved state of the netlogon creds. */
788                         BOOL ret;
789
790                         become_root();
791                         ret = secrets_restore_schannel_session_info(p->pipe_state_mem_ctx,
792                                         remote_machine,
793                                         &p->dc);
794                         unbecome_root();
795                         if (!ret) {
796                                 return NT_STATUS_INVALID_HANDLE;
797                         }
798                 }
799
800                 if (!p->dc || !p->dc->authenticated) {
801                         return NT_STATUS_INVALID_HANDLE;
802                 }
803
804                 /* checks and updates credentials.  creates reply credentials */
805                 if (!creds_server_step(p->dc, &q_u->sam_id.client.cred,  &r_u->srv_creds)) {
806                         DEBUG(2,("_net_sam_logon: creds_server_step failed. Rejecting auth "
807                                 "request from client %s machine account %s\n",
808                                 remote_machine, p->dc->mach_acct ));
809                         return NT_STATUS_INVALID_PARAMETER;
810                 }
811
812                 /* We must store the creds state after an update. */
813                 become_root();
814                 secrets_store_schannel_session_info(p->pipe_state_mem_ctx,
815                                         remote_machine,
816                                         p->dc);
817                 unbecome_root();
818         }
819
820         switch (q_u->sam_id.logon_level) {
821         case INTERACTIVE_LOGON_TYPE:
822                 uni_samlogon_user = &ctr->auth.id1.uni_user_name;
823                 uni_samlogon_domain = &ctr->auth.id1.uni_domain_name;
824
825                 uni_samlogon_workstation = &ctr->auth.id1.uni_wksta_name;
826             
827                 DEBUG(3,("SAM Logon (Interactive). Domain:[%s].  ", lp_workgroup()));
828                 break;
829         case NET_LOGON_TYPE:
830                 uni_samlogon_user = &ctr->auth.id2.uni_user_name;
831                 uni_samlogon_domain = &ctr->auth.id2.uni_domain_name;
832                 uni_samlogon_workstation = &ctr->auth.id2.uni_wksta_name;
833             
834                 DEBUG(3,("SAM Logon (Network). Domain:[%s].  ", lp_workgroup()));
835                 break;
836         default:
837                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
838                 return NT_STATUS_INVALID_INFO_CLASS;
839         } /* end switch */
840
841         rpcstr_pull(nt_username,uni_samlogon_user->buffer,sizeof(nt_username),uni_samlogon_user->uni_str_len*2,0);
842         rpcstr_pull(nt_domain,uni_samlogon_domain->buffer,sizeof(nt_domain),uni_samlogon_domain->uni_str_len*2,0);
843         rpcstr_pull(nt_workstation,uni_samlogon_workstation->buffer,sizeof(nt_workstation),uni_samlogon_workstation->uni_str_len*2,0);
844
845         DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
846         fstrcpy(current_user_info.smb_name, nt_username);
847         sub_set_smb_name(nt_username);
848      
849         DEBUG(5,("Attempting validation level %d for unmapped username %s.\n", q_u->sam_id.ctr->switch_value, nt_username));
850
851         status = NT_STATUS_OK;
852         
853         switch (ctr->switch_value) {
854         case NET_LOGON_TYPE:
855         {
856                 const char *wksname = nt_workstation;
857                 
858                 if (!NT_STATUS_IS_OK(status = make_auth_context_fixed(&auth_context, ctr->auth.id2.lm_chal))) {
859                         return status;
860                 }
861
862                 /* For a network logon, the workstation name comes in with two
863                  * backslashes in the front. Strip them if they are there. */
864
865                 if (*wksname == '\\') wksname++;
866                 if (*wksname == '\\') wksname++;
867
868                 /* Standard challenge/response authenticaion */
869                 if (!make_user_info_netlogon_network(&user_info, 
870                                                      nt_username, nt_domain, 
871                                                      wksname,
872                                                      ctr->auth.id2.param_ctrl,
873                                                      ctr->auth.id2.lm_chal_resp.buffer,
874                                                      ctr->auth.id2.lm_chal_resp.str_str_len,
875                                                      ctr->auth.id2.nt_chal_resp.buffer,
876                                                      ctr->auth.id2.nt_chal_resp.str_str_len)) {
877                         status = NT_STATUS_NO_MEMORY;
878                 }       
879                 break;
880         }
881         case INTERACTIVE_LOGON_TYPE:
882                 /* 'Interactive' authentication, supplies the password in its
883                    MD4 form, encrypted with the session key.  We will convert
884                    this to challenge/response for the auth subsystem to chew
885                    on */
886         {
887                 const uint8 *chal;
888                 
889                 if (!NT_STATUS_IS_OK(status = make_auth_context_subsystem(&auth_context))) {
890                         return status;
891                 }
892                 
893                 chal = auth_context->get_ntlm_challenge(auth_context);
894
895                 if (!make_user_info_netlogon_interactive(&user_info, 
896                                                          nt_username, nt_domain, 
897                                                          nt_workstation, 
898                                                          ctr->auth.id1.param_ctrl,
899                                                          chal,
900                                                          ctr->auth.id1.lm_owf.data, 
901                                                          ctr->auth.id1.nt_owf.data, 
902                                                          p->dc->sess_key)) {
903                         status = NT_STATUS_NO_MEMORY;
904                 }
905                 break;
906         }
907         default:
908                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
909                 return NT_STATUS_INVALID_INFO_CLASS;
910         } /* end switch */
911         
912         if ( NT_STATUS_IS_OK(status) ) {
913                 status = auth_context->check_ntlm_password(auth_context, 
914                         user_info, &server_info);
915         }
916
917         (auth_context->free)(&auth_context);    
918         free_user_info(&user_info);
919         
920         DEBUG(5, ("_net_sam_logon: check_password returned status %s\n", 
921                   nt_errstr(status)));
922
923         /* Check account and password */
924     
925         if (!NT_STATUS_IS_OK(status)) {
926                 /* If we don't know what this domain is, we need to 
927                    indicate that we are not authoritative.  This 
928                    allows the client to decide if it needs to try 
929                    a local user.  Fix by jpjanosi@us.ibm.com, #2976 */
930                 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) 
931                      && !strequal(nt_domain, get_global_sam_name())
932                      && !is_trusted_domain(nt_domain) )
933                         r_u->auth_resp = 0; /* We are not authoritative */
934
935                 TALLOC_FREE(server_info);
936                 return status;
937         }
938
939         if (server_info->guest) {
940                 /* We don't like guest domain logons... */
941                 DEBUG(5,("_net_sam_logon: Attempted domain logon as GUEST "
942                          "denied.\n"));
943                 TALLOC_FREE(server_info);
944                 return NT_STATUS_LOGON_FAILURE;
945         }
946
947         /* This is the point at which, if the login was successful, that
948            the SAM Local Security Authority should record that the user is
949            logged in to the domain.  */
950     
951         {
952                 DOM_GID *gids = NULL;
953                 const DOM_SID *user_sid = NULL;
954                 const DOM_SID *group_sid = NULL;
955                 DOM_SID domain_sid;
956                 uint32 user_rid, group_rid; 
957
958                 int num_gids = 0;
959                 pstring my_name;
960                 fstring user_sid_string;
961                 fstring group_sid_string;
962                 unsigned char user_session_key[16];
963                 unsigned char lm_session_key[16];
964                 unsigned char pipe_session_key[16];
965
966                 sampw = server_info->sam_account;
967
968                 /* set up pointer indicating user/password failed to be
969                  * found */
970                 usr_info->ptr_user_info = 0;
971
972                 user_sid = pdb_get_user_sid(sampw);
973                 group_sid = pdb_get_group_sid(sampw);
974
975                 if ((user_sid == NULL) || (group_sid == NULL)) {
976                         DEBUG(1, ("_net_sam_logon: User without group or user SID\n"));
977                         return NT_STATUS_UNSUCCESSFUL;
978                 }
979
980                 sid_copy(&domain_sid, user_sid);
981                 sid_split_rid(&domain_sid, &user_rid);
982
983                 if (!sid_peek_check_rid(&domain_sid, group_sid, &group_rid)) {
984                         DEBUG(1, ("_net_sam_logon: user %s\\%s has user sid "
985                                   "%s\n but group sid %s.\n"
986                                   "The conflicting domain portions are not "
987                                   "supported for NETLOGON calls\n",         
988                                   pdb_get_domain(sampw),
989                                   pdb_get_username(sampw),
990                                   sid_to_string(user_sid_string, user_sid),
991                                   sid_to_string(group_sid_string, group_sid)));
992                         return NT_STATUS_UNSUCCESSFUL;
993                 }
994                 
995                 
996                 if(server_info->login_server) {
997                         pstrcpy(my_name, server_info->login_server);
998                 } else {
999                         pstrcpy(my_name, global_myname());
1000                 }
1001
1002                 status = nt_token_to_group_list(p->mem_ctx, &domain_sid,
1003                                                 server_info->num_sids,
1004                                                 server_info->sids,
1005                                                 &num_gids, &gids);
1006
1007                 if (!NT_STATUS_IS_OK(status)) {
1008                         return status;
1009                 }
1010
1011                 if (server_info->user_session_key.length) {
1012                         memcpy(user_session_key,
1013                                server_info->user_session_key.data, 
1014                                MIN(sizeof(user_session_key),
1015                                    server_info->user_session_key.length));
1016                         if (process_creds) {
1017                                 /* Get the pipe session key from the creds. */
1018                                 memcpy(pipe_session_key, p->dc->sess_key, 16);
1019                         } else {
1020                                 /* Get the pipe session key from the schannel. */
1021                                 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL || p->auth.a_u.schannel_auth == NULL) {
1022                                         return NT_STATUS_INVALID_HANDLE;
1023                                 }
1024                                 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->sess_key, 16);
1025                         }
1026                         SamOEMhash(user_session_key, pipe_session_key, 16);
1027                         memset(pipe_session_key, '\0', 16);
1028                 }
1029                 if (server_info->lm_session_key.length) {
1030                         memcpy(lm_session_key,
1031                                server_info->lm_session_key.data, 
1032                                MIN(sizeof(lm_session_key),
1033                                    server_info->lm_session_key.length));
1034                         if (process_creds) {
1035                                 /* Get the pipe session key from the creds. */
1036                                 memcpy(pipe_session_key, p->dc->sess_key, 16);
1037                         } else {
1038                                 /* Get the pipe session key from the schannel. */
1039                                 if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL || p->auth.a_u.schannel_auth == NULL) {
1040                                         return NT_STATUS_INVALID_HANDLE;
1041                                 }
1042                                 memcpy(pipe_session_key, p->auth.a_u.schannel_auth->sess_key, 16);
1043                         }
1044                         SamOEMhash(lm_session_key, pipe_session_key, 16);
1045                         memset(pipe_session_key, '\0', 16);
1046                 }
1047                 
1048                 init_net_user_info3(p->mem_ctx, usr_info, 
1049                                     user_rid,
1050                                     group_rid,   
1051                                     pdb_get_username(sampw),
1052                                     pdb_get_fullname(sampw),
1053                                     pdb_get_homedir(sampw),
1054                                     pdb_get_dir_drive(sampw),
1055                                     pdb_get_logon_script(sampw),
1056                                     pdb_get_profile_path(sampw),
1057                                     pdb_get_logon_time(sampw),
1058                                     get_time_t_max(),
1059                                     get_time_t_max(),
1060                                     pdb_get_pass_last_set_time(sampw),
1061                                     pdb_get_pass_can_change_time(sampw),
1062                                     pdb_get_pass_must_change_time(sampw),
1063                                     0, /* logon_count */
1064                                     0, /* bad_pw_count */
1065                                     num_gids,    /* uint32 num_groups */
1066                                     gids    , /* DOM_GID *gids */
1067                                     LOGON_EXTRA_SIDS, /* uint32 user_flgs (?) */
1068                                     pdb_get_acct_ctrl(sampw),
1069                                     server_info->user_session_key.length ? user_session_key : NULL,
1070                                     server_info->lm_session_key.length ? lm_session_key : NULL,
1071                                     my_name     , /* char *logon_srv */
1072                                     pdb_get_domain(sampw),
1073                                     &domain_sid);     /* DOM_SID *dom_sid */  
1074                 ZERO_STRUCT(user_session_key);
1075                 ZERO_STRUCT(lm_session_key);
1076         }
1077         TALLOC_FREE(server_info);
1078         return status;
1079 }
1080
1081 /*************************************************************************
1082  _net_sam_logon
1083  *************************************************************************/
1084
1085 NTSTATUS _net_sam_logon(pipes_struct *p, NET_Q_SAM_LOGON *q_u, NET_R_SAM_LOGON *r_u)
1086 {
1087         return _net_sam_logon_internal(p, q_u, r_u, True);
1088 }
1089  
1090 /*************************************************************************
1091  _net_sam_logon_ex - no credential chaining. Map into net sam logon.
1092  *************************************************************************/
1093
1094 NTSTATUS _net_sam_logon_ex(pipes_struct *p, NET_Q_SAM_LOGON_EX *q_u, NET_R_SAM_LOGON_EX *r_u)
1095 {
1096         NET_Q_SAM_LOGON q;
1097         NET_R_SAM_LOGON r;
1098
1099         ZERO_STRUCT(q);
1100         ZERO_STRUCT(r);
1101
1102         /* Only allow this if the pipe is protected. */
1103         if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1104                 DEBUG(0,("_net_sam_logon_ex: client %s not using schannel for netlogon\n",
1105                         get_remote_machine_name() ));
1106                 return NT_STATUS_INVALID_PARAMETER;
1107         }
1108
1109         /* Map a NET_Q_SAM_LOGON_EX to NET_Q_SAM_LOGON. */
1110         q.validation_level = q_u->validation_level;
1111
1112         /* Map a DOM_SAM_INFO_EX into a DOM_SAM_INFO with no creds. */
1113         q.sam_id.client.login = q_u->sam_id.client;
1114         q.sam_id.logon_level = q_u->sam_id.logon_level;
1115         q.sam_id.ctr = q_u->sam_id.ctr;
1116
1117         r_u->status = _net_sam_logon_internal(p, &q, &r, False);
1118
1119         if (!NT_STATUS_IS_OK(r_u->status)) {
1120                 return r_u->status;
1121         }
1122
1123         /* Map the NET_R_SAM_LOGON to NET_R_SAM_LOGON_EX. */
1124         r_u->switch_value = r.switch_value;
1125         r_u->user = r.user;
1126         r_u->auth_resp = r.auth_resp;
1127         r_u->flags = 0; /* FIXME ! */
1128         return r_u->status;
1129 }
1130
1131 /*************************************************************************
1132  _ds_enum_dom_trusts
1133  *************************************************************************/
1134 #if 0   /* JERRY -- not correct */
1135 NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1136                              DS_R_ENUM_DOM_TRUSTS *r_u)
1137 {
1138         NTSTATUS status = NT_STATUS_OK;
1139
1140         /* TODO: According to MSDN, the can only be executed against a 
1141            DC or domain member running Windows 2000 or later.  Need
1142            to test against a standalone 2k server and see what it 
1143            does.  A windows 2000 DC includes its own domain in the 
1144            list.  --jerry */
1145
1146         return status;
1147 }
1148 #endif  /* JERRY */