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