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