r2635: mem_ctx cleanups on the lsa and netlogon pipes in the rpc server
[tprouty/samba.git] / source4 / rpc_server / netlogon / dcerpc_netlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    endpoint server for the netlogon pipe
5
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "rpc_server/common/common.h"
25
26 struct server_pipe_state {
27         struct netr_Credential client_challenge;
28         struct netr_Credential server_challenge;
29         BOOL authenticated;
30         char *account_name;
31         char *computer_name;  /* for logging only */
32         uint32_t acct_flags;
33         uint16_t sec_chan_type;
34         struct creds_CredentialState *creds;
35 };
36
37
38 /*
39   a client has connected to the netlogon server using schannel, so we need
40   to re-establish the credentials state
41 */
42 static NTSTATUS netlogon_schannel_setup(struct dcesrv_call_state *dce_call) 
43 {
44         struct server_pipe_state *state;
45         NTSTATUS status;
46
47         state = talloc_p(dce_call->conn, struct server_pipe_state);
48         if (state == NULL) {
49                 return NT_STATUS_NO_MEMORY;
50         }
51         ZERO_STRUCTP(state);
52         state->authenticated = True;
53         
54         if (dce_call->conn->auth_state.session_info == NULL) {
55                 talloc_free(state);
56                 smb_panic("No session info provided by schannel level setup!");
57                 return NT_STATUS_NO_USER_SESSION_KEY;
58         }
59         
60         status = dcerpc_schannel_creds(dce_call->conn->auth_state.gensec_security, 
61                                        state, 
62                                        &state->creds);
63
64         if (!NT_STATUS_IS_OK(status)) {
65                 DEBUG(3, ("getting schannel credentials failed with %s\n", nt_errstr(status)));
66                 talloc_free(state);
67                 return status;
68         }
69         
70         dce_call->conn->private = state;
71
72         return NT_STATUS_OK;
73 }
74
75 /*
76   a hook for bind on the netlogon pipe
77 */
78 static NTSTATUS netlogon_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *di) 
79 {
80         dce_call->conn->private = NULL;
81
82         /* if this is a schannel bind then we need to reconstruct the pipe state */
83         if (dce_call->conn->auth_state.auth_info &&
84             dce_call->conn->auth_state.auth_info->auth_type == DCERPC_AUTH_TYPE_SCHANNEL) {
85                 NTSTATUS status;
86
87                 DEBUG(5, ("schannel bind on netlogon\n"));
88
89                 status = netlogon_schannel_setup(dce_call);
90                 if (!NT_STATUS_IS_OK(status)) {
91                         DEBUG(3, ("schannel bind on netlogon failed with %s\n", nt_errstr(status)));
92                         return status;
93                 }
94         }
95
96         return NT_STATUS_OK;
97 }
98
99 /* this function is called when the client disconnects the endpoint */
100 static void netlogon_unbind(struct dcesrv_connection *conn, const struct dcesrv_interface *di) 
101 {
102         struct server_pipe_state *pipe_state = conn->private;
103
104         if (pipe_state) {
105                 talloc_free(pipe_state);
106         }
107
108         conn->private = NULL;
109 }
110
111 #define DCESRV_INTERFACE_NETLOGON_BIND netlogon_bind
112 #define DCESRV_INTERFACE_NETLOGON_UNBIND netlogon_unbind
113
114 static NTSTATUS netr_ServerReqChallenge(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
115                                         struct netr_ServerReqChallenge *r)
116 {
117         struct server_pipe_state *pipe_state = dce_call->conn->private;
118
119         ZERO_STRUCTP(r->out.credentials);
120
121         /* destroyed on pipe shutdown */
122
123         if (pipe_state) {
124                 talloc_free(pipe_state);
125                 dce_call->conn->private = NULL;
126         }
127         
128         pipe_state = talloc_p(dce_call->conn, struct server_pipe_state);
129         if (!pipe_state) {
130                 return NT_STATUS_NO_MEMORY;
131         }
132
133         pipe_state->authenticated = False;
134         pipe_state->creds = NULL;
135         pipe_state->account_name = NULL;
136         pipe_state->computer_name = NULL;
137
138         pipe_state->client_challenge = *r->in.credentials;
139
140         generate_random_buffer(pipe_state->server_challenge.data, 
141                                sizeof(pipe_state->server_challenge.data));
142
143         *r->out.credentials = pipe_state->server_challenge;
144
145         dce_call->conn->private = pipe_state;
146
147         return NT_STATUS_OK;
148 }
149
150 static NTSTATUS netr_ServerAuthenticate3(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
151                                          struct netr_ServerAuthenticate3 *r)
152 {
153         struct server_pipe_state *pipe_state = dce_call->conn->private;
154         void *sam_ctx;
155         struct samr_Password *mach_pwd;
156         uint16_t acct_flags;
157         int num_records;
158         struct ldb_message **msgs;
159         NTSTATUS nt_status;
160         const char *attrs[] = {"unicodePwd", "lmPwdHash", "ntPwdHash", "userAccountControl", 
161                                "objectSid", NULL};
162
163         ZERO_STRUCTP(r->out.credentials);
164         *r->out.rid = 0;
165         *r->out.negotiate_flags = *r->in.negotiate_flags;
166
167         if (!pipe_state) {
168                 DEBUG(1, ("No challenge requested by client, cannot authenticate\n"));
169                 return NT_STATUS_ACCESS_DENIED;
170         }
171
172         sam_ctx = samdb_connect(mem_ctx);
173         if (sam_ctx == NULL) {
174                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
175         }
176         /* pull the user attributes */
177         num_records = samdb_search(sam_ctx, mem_ctx, NULL, &msgs, attrs,
178                                    "(&(sAMAccountName=%s)(objectclass=user))", 
179                                    r->in.account_name);
180
181         if (num_records == 0) {
182                 DEBUG(3,("Couldn't find user [%s] in samdb.\n", 
183                          r->in.account_name));
184                 return NT_STATUS_NO_SUCH_USER;
185         }
186
187         if (num_records > 1) {
188                 DEBUG(0,("Found %d records matching user [%s]\n", num_records, r->in.account_name));
189                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
190         }
191
192         acct_flags = samdb_result_acct_flags(msgs[0], 
193                                              "userAccountControl");
194
195         if (acct_flags & ACB_DISABLED) {
196                 DEBUG(1, ("Account [%s] is disabled\n", r->in.account_name));
197                 return NT_STATUS_ACCESS_DENIED;
198         }
199
200         if (r->in.secure_channel_type == SEC_CHAN_WKSTA) {
201                 if (!(acct_flags & ACB_WSTRUST)) {
202                         DEBUG(1, ("Client asked for a workstation secure channel, but is not a workstation (member server) acb flags: 0x%x\n", acct_flags));
203                         return NT_STATUS_ACCESS_DENIED;
204                 }
205         } else if (r->in.secure_channel_type == SEC_CHAN_DOMAIN) {
206                 if (!(acct_flags & ACB_DOMTRUST)) {
207                         DEBUG(1, ("Client asked for a trusted domain secure channel, but is not a trusted domain: acb flags: 0x%x\n", acct_flags));
208                         return NT_STATUS_ACCESS_DENIED;
209                 }
210         } else if (r->in.secure_channel_type == SEC_CHAN_BDC) {
211                 if (!(acct_flags & ACB_SVRTRUST)) {
212                         DEBUG(1, ("Client asked for a server secure channel, but is not a server (domain controller): acb flags: 0x%x\n", acct_flags));
213                         return NT_STATUS_ACCESS_DENIED;
214                 }
215         } else {
216                 DEBUG(1, ("Client asked for an invalid secure channel type: %d\n", 
217                           r->in.secure_channel_type));
218                 return NT_STATUS_ACCESS_DENIED;
219         }
220
221         pipe_state->acct_flags = acct_flags;
222         pipe_state->sec_chan_type = r->in.secure_channel_type;
223
224         *r->out.rid = samdb_result_rid_from_sid(mem_ctx, msgs[0], "objectSid", 0);
225
226         nt_status = samdb_result_passwords(mem_ctx, msgs[0], NULL, &mach_pwd);
227         if (!NT_STATUS_IS_OK(nt_status) || mach_pwd == NULL) {
228                 return NT_STATUS_ACCESS_DENIED;
229         }
230
231         if (!pipe_state->creds) {
232                 pipe_state->creds = talloc_p(pipe_state, struct creds_CredentialState);
233                 if (!pipe_state->creds) {
234                         return NT_STATUS_NO_MEMORY;
235                 }
236         }
237
238         creds_server_init(pipe_state->creds, &pipe_state->client_challenge, 
239                           &pipe_state->server_challenge, mach_pwd,
240                           r->out.credentials,
241                           *r->in.negotiate_flags);
242         
243         if (!creds_server_check(pipe_state->creds, r->in.credentials)) {
244                 return NT_STATUS_ACCESS_DENIED;
245         }
246
247         pipe_state->authenticated = True;
248
249         if (pipe_state->account_name) {
250                 /* We don't want a memory leak on this long-lived talloc context */
251                 talloc_free(pipe_state->account_name);
252         }
253
254         pipe_state->account_name = talloc_strdup(pipe_state, r->in.account_name);
255         
256         if (pipe_state->computer_name) {
257                 /* We don't want a memory leak on this long-lived talloc context */
258                 talloc_free(pipe_state->account_name);
259         }
260
261         pipe_state->computer_name = talloc_strdup(pipe_state, r->in.computer_name);
262
263         /* remember this session key state */
264         nt_status = schannel_store_session_key(mem_ctx, pipe_state->computer_name, pipe_state->creds);
265
266         return nt_status;
267 }
268                                                  
269 static NTSTATUS netr_ServerAuthenticate(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
270                                         struct netr_ServerAuthenticate *r)
271 {
272         struct netr_ServerAuthenticate3 r3;
273         uint32_t rid = 0;
274         /* TODO: 
275          * negotiate_flags is used as an [in] parameter
276          * so it need to be initialised.
277          *
278          * (I think ... = 0; seems wrong here --metze)
279          */
280         uint32 negotiate_flags = 0;  
281
282         r3.in.server_name = r->in.server_name;
283         r3.in.account_name = r->in.account_name;
284         r3.in.secure_channel_type = r->in.secure_channel_type;
285         r3.in.computer_name = r->in.computer_name;
286         r3.in.credentials = r->in.credentials;
287         r3.out.credentials = r->out.credentials;
288         r3.in.negotiate_flags = &negotiate_flags;
289         r3.out.negotiate_flags = &negotiate_flags;
290         r3.out.rid = &rid;
291         
292         return netr_ServerAuthenticate3(dce_call, mem_ctx, &r3);
293 }
294
295 static NTSTATUS netr_ServerAuthenticate2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
296                                          struct netr_ServerAuthenticate2 *r)
297 {
298         struct netr_ServerAuthenticate3 r3;
299         uint32 rid = 0;
300
301         r3.in.server_name = r->in.server_name;
302         r3.in.account_name = r->in.account_name;
303         r3.in.secure_channel_type = r->in.secure_channel_type;
304         r3.in.computer_name = r->in.computer_name;
305         r3.in.credentials = r->in.credentials;
306         r3.out.credentials = r->out.credentials;
307         r3.in.negotiate_flags = r->in.negotiate_flags;
308         r3.out.negotiate_flags = r->out.negotiate_flags;
309         r3.out.rid = &rid;
310         
311         return netr_ServerAuthenticate3(dce_call, mem_ctx, &r3);
312 }
313
314
315 static BOOL netr_creds_server_step_check(struct server_pipe_state *pipe_state,
316                                          struct netr_Authenticator *received_authenticator,
317                                          struct netr_Authenticator *return_authenticator) 
318 {
319         if (!pipe_state->authenticated) {
320                 return False;
321         }
322         return creds_server_step_check(pipe_state->creds, 
323                                        received_authenticator, 
324                                        return_authenticator);
325 }
326
327
328 static NTSTATUS netr_ServerPasswordSet(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
329                                        struct netr_ServerPasswordSet *r)
330 {
331         struct server_pipe_state *pipe_state = dce_call->conn->private;
332
333         void *sam_ctx;
334         int num_records;
335         int num_records_domain;
336         int ret;
337         struct ldb_message **msgs;
338         struct ldb_message **msgs_domain;
339         NTSTATUS nt_status;
340         struct ldb_message mod, *msg_set_pw = &mod;
341         const char *domain_dn;
342         const char *domain_sid;
343
344         const char *attrs[] = {"objectSid", NULL };
345
346         const char **domain_attrs = attrs;
347         ZERO_STRUCT(mod);
348
349         if (!pipe_state) {
350                 DEBUG(1, ("No challenge requested by client, cannot authenticate\n"));
351                 return NT_STATUS_ACCESS_DENIED;
352         }
353
354         if (!netr_creds_server_step_check(pipe_state, &r->in.credential, &r->out.return_authenticator)) {
355                 return NT_STATUS_ACCESS_DENIED;
356         }
357
358         sam_ctx = samdb_connect(mem_ctx);
359         if (sam_ctx == NULL) {
360                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
361         }
362         /* pull the user attributes */
363         num_records = samdb_search(sam_ctx, mem_ctx, NULL, &msgs, attrs,
364                                    "(&(sAMAccountName=%s)(objectclass=user))", 
365                                    pipe_state->account_name);
366
367         if (num_records == 0) {
368                 DEBUG(3,("Couldn't find user [%s] in samdb.\n", 
369                          pipe_state->account_name));
370                 return NT_STATUS_NO_SUCH_USER;
371         }
372
373         if (num_records > 1) {
374                 DEBUG(0,("Found %d records matching user [%s]\n", num_records, 
375                          pipe_state->account_name));
376                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
377         }
378
379         domain_sid = samdb_result_sid_prefix(mem_ctx, msgs[0], "objectSid");
380         if (!domain_sid) {
381                 DEBUG(0,("no objectSid in user record\n"));
382                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
383         }
384
385         /* find the domain's DN */
386         num_records_domain = samdb_search(sam_ctx, mem_ctx, NULL, 
387                                           &msgs_domain, domain_attrs,
388                                           "(&(objectSid=%s)(objectclass=domain))", 
389                                           domain_sid);
390
391         if (num_records_domain == 0) {
392                 DEBUG(3,("check_sam_security: Couldn't find domain [%s] in passdb file.\n", 
393                          domain_sid));
394                 return NT_STATUS_NO_SUCH_USER;
395         }
396
397         if (num_records_domain > 1) {
398                 DEBUG(0,("Found %d records matching domain [%s]\n", 
399                          num_records_domain, domain_sid));
400                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
401         }
402
403         domain_dn = msgs_domain[0]->dn;
404         
405         mod.dn = talloc_strdup(mem_ctx, msgs[0]->dn);
406         if (!mod.dn) {
407                 return NT_STATUS_NO_MEMORY;
408         }
409         
410         creds_des_decrypt(pipe_state->creds, &r->in.new_password);
411
412         /* set the password - samdb needs to know both the domain and user DNs,
413            so the domain password policy can be used */
414         nt_status = samdb_set_password(sam_ctx, mem_ctx,
415                                        msgs[0]->dn, domain_dn,
416                                        msg_set_pw, 
417                                        NULL, /* Don't have plaintext */
418                                        NULL, &r->in.new_password,
419                                        False /* This is not considered a password change */,
420                                        NULL);
421         
422         if (!NT_STATUS_IS_OK(nt_status)) {
423                 return nt_status;
424         }
425
426         ret = samdb_replace(sam_ctx, mem_ctx, msg_set_pw);
427         if (ret != 0) {
428                 /* we really need samdb.c to return NTSTATUS */
429                 return NT_STATUS_UNSUCCESSFUL;
430         }
431
432         return NT_STATUS_OK;
433 }
434
435
436 /* 
437   netr_LogonUasLogon 
438 */
439 static WERROR netr_LogonUasLogon(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
440                                  struct netr_LogonUasLogon *r)
441 {
442         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
443 }
444
445
446 /* 
447   netr_LogonUasLogoff 
448 */
449 static WERROR netr_LogonUasLogoff(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
450                        struct netr_LogonUasLogoff *r)
451 {
452         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
453 }
454
455
456 /* 
457   netr_LogonSamLogonWithFlags
458
459 */
460 static NTSTATUS netr_LogonSamLogonWithFlags(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
461                                             struct netr_LogonSamLogonWithFlags *r)
462 {
463         struct server_pipe_state *pipe_state = dce_call->conn->private;
464
465         struct auth_context *auth_context;
466         struct auth_usersupplied_info *user_info;
467         struct auth_serversupplied_info *server_info;
468         NTSTATUS nt_status;
469         const uint8_t *chal;
470         static const char zeros[16];
471         struct netr_SamBaseInfo *sam;
472         struct netr_SamInfo2 *sam2;
473         struct netr_SamInfo3 *sam3;
474         struct netr_SamInfo6 *sam6;
475         
476         if (!pipe_state) {
477                 DEBUG(1, ("No challenge requested by client, cannot authenticate\n"));
478                 return NT_STATUS_ACCESS_DENIED;
479         }
480
481         r->out.return_authenticator = talloc_p(mem_ctx, struct netr_Authenticator);
482         if (!r->out.return_authenticator) {
483                 return NT_STATUS_NO_MEMORY;
484         }
485
486         if (!netr_creds_server_step_check(pipe_state, r->in.credential, r->out.return_authenticator)) {
487                 return NT_STATUS_ACCESS_DENIED;
488         }
489
490         switch (r->in.logon_level) {
491         case 1:
492         case 3:
493         case 5:
494                 creds_arcfour_crypt(pipe_state->creds, 
495                                     r->in.logon.password->lmpassword.hash, 
496                                     sizeof(r->in.logon.password->lmpassword.hash));
497                 creds_arcfour_crypt(pipe_state->creds, 
498                                     r->in.logon.password->ntpassword.hash, 
499                                     sizeof(r->in.logon.password->ntpassword.hash));
500
501                 nt_status = make_auth_context_subsystem(&auth_context);
502                 if (!NT_STATUS_IS_OK(nt_status)) {
503                         return nt_status;
504                 }
505
506                 chal = auth_context->get_ntlm_challenge(auth_context);
507                 nt_status = make_user_info_netlogon_interactive(&user_info,
508                                                                 r->in.logon.password->identity_info.account_name.string,
509                                                                 r->in.logon.password->identity_info.domain_name.string,
510                                                                 r->in.logon.password->identity_info.workstation.string,
511                                                                 chal,
512                                                                 &r->in.logon.password->lmpassword,
513                                                                 &r->in.logon.password->ntpassword);
514                 break;
515                 
516         case 2:
517         case 6:
518                 nt_status = make_auth_context_fixed(&auth_context, r->in.logon.network->challenge);
519                 if (!NT_STATUS_IS_OK(nt_status)) {
520                         return nt_status;
521                 }
522
523                 nt_status = make_user_info_netlogon_network(&user_info,
524                                                             r->in.logon.network->identity_info.account_name.string,
525                                                             r->in.logon.network->identity_info.domain_name.string,
526                                                             r->in.logon.network->identity_info.workstation.string,
527                                                             r->in.logon.network->lm.data, r->in.logon.network->lm.length,
528                                                             r->in.logon.network->nt.data, r->in.logon.network->nt.length);
529                 break;
530         default:
531                 free_auth_context(&auth_context);
532                 return NT_STATUS_INVALID_PARAMETER;
533         }
534         
535         if (!NT_STATUS_IS_OK(nt_status)) {
536                 return nt_status;
537         }
538
539         nt_status = auth_context->check_ntlm_password(auth_context,
540                                                       user_info, 
541                                                       &server_info);
542
543         if (!NT_STATUS_IS_OK(nt_status)) {
544                 free_auth_context(&auth_context);
545                 return nt_status;
546         }
547         free_auth_context(&auth_context);
548
549         sam = talloc_p(mem_ctx, struct netr_SamBaseInfo);
550
551         ZERO_STRUCTP(sam);
552         
553         sam->last_logon = server_info->last_logon;
554         sam->last_logoff = server_info->last_logoff;
555         sam->acct_expiry = server_info->acct_expiry;
556         sam->last_password_change = server_info->last_password_change;
557         sam->allow_password_change = server_info->allow_password_change;
558         sam->force_password_change = server_info->force_password_change;
559         
560         sam->account_name.string = talloc_strdup(mem_ctx, server_info->account_name);
561         sam->full_name.string = talloc_strdup(mem_ctx, server_info->full_name);
562         sam->logon_script.string = talloc_strdup(mem_ctx, server_info->logon_script);
563         sam->profile_path.string = talloc_strdup(mem_ctx, server_info->profile_path);
564         sam->home_directory.string = talloc_strdup(mem_ctx, server_info->home_directory);
565         sam->home_drive.string = talloc_strdup(mem_ctx, server_info->home_drive);
566         
567         sam->logon_count = server_info->logon_count;
568         sam->bad_password_count = sam->bad_password_count;
569         sam->rid = server_info->user_sid->sub_auths[server_info->user_sid->num_auths-1];
570         sam->primary_gid = server_info->primary_group_sid->sub_auths[server_info->primary_group_sid->num_auths-1];
571         sam->group_count = 0;
572         sam->groupids = NULL;
573         sam->user_flags = 0; /* TODO: w2k3 uses 0x120 - what is this? */
574         sam->acct_flags = server_info->acct_flags;      
575         sam->logon_server.string = lp_netbios_name();
576         
577         sam->domain.string = talloc_strdup(mem_ctx, server_info->domain);
578         
579         sam->domain_sid = dom_sid_dup(mem_ctx, server_info->user_sid);
580         sam->domain_sid->num_auths--;
581
582         if (server_info->user_session_key.length == sizeof(sam->key.key)) {
583                 memcpy(sam->key.key, server_info->user_session_key.data, sizeof(sam->key.key));
584         } else {
585                 ZERO_STRUCT(sam->key.key);
586         }
587         
588         /* Don't crypt an all-zero key, it would give away the NETLOGON pipe session key */
589         /* It appears that level 6 is not individually encrypted */
590         if ((r->in.validation_level != 6) 
591             && memcmp(sam->key.key, zeros,  
592                       sizeof(sam->key.key)) != 0) {
593                 creds_arcfour_crypt(pipe_state->creds, 
594                                     sam->key.key, 
595                                     sizeof(sam->key.key));
596         }
597         
598         if (server_info->lm_session_key.length == sizeof(sam->LMSessKey.key)) {
599                 memcpy(sam->LMSessKey.key, server_info->lm_session_key.data, 
600                        sizeof(sam->LMSessKey.key));
601         } else {
602                 ZERO_STRUCT(sam->LMSessKey.key);
603         }
604         
605         /* Don't crypt an all-zero key, it would give away the NETLOGON pipe session key */
606         /* It appears that level 6 is not individually encrypted */
607         if ((r->in.validation_level != 6) 
608             && memcmp(sam->LMSessKey.key, zeros,  
609                       sizeof(sam->LMSessKey.key)) != 0) {
610                 creds_arcfour_crypt(pipe_state->creds, 
611                                     sam->LMSessKey.key, 
612                                     sizeof(sam->LMSessKey.key));
613         }
614
615         switch (r->in.validation_level) {
616         case 2:
617                 sam2 = talloc_p(mem_ctx, struct netr_SamInfo2);
618                 ZERO_STRUCTP(sam2);
619                 sam2->base = *sam;
620                 r->out.validation.sam2 = sam2;
621                 break;
622
623         case 3:
624                 sam3 = talloc_p(mem_ctx, struct netr_SamInfo3);
625                 ZERO_STRUCTP(sam3);
626                 sam3->base = *sam;
627                 r->out.validation.sam3 = sam3;
628                 break;
629
630         case 6:
631                 sam6 = talloc_p(mem_ctx, struct netr_SamInfo6);
632                 ZERO_STRUCTP(sam6);
633                 sam6->base = *sam;
634                 sam6->forest.string = lp_realm();
635                 sam6->principle.string = talloc_asprintf(mem_ctx, "%s@%s", 
636                                                          sam->account_name.string, sam6->forest.string);
637                 r->out.validation.sam6 = sam6;
638                 break;
639
640         default:
641                 break;
642         }
643
644         r->out.authoritative = 1;
645
646         return NT_STATUS_OK;
647 }
648
649 /* 
650   netr_LogonSamLogon
651 */
652 static NTSTATUS netr_LogonSamLogon(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
653                                    struct netr_LogonSamLogon *r)
654 {
655         struct netr_LogonSamLogonWithFlags r2;
656         NTSTATUS status;
657
658         ZERO_STRUCT(r2);
659
660         r2.in.server_name = r->in.server_name;
661         r2.in.workstation = r->in.workstation;
662         r2.in.credential  = r->in.credential;
663         r2.in.return_authenticator = r->in.return_authenticator;
664         r2.in.logon_level = r->in.logon_level;
665         r2.in.logon = r->in.logon;
666         r2.in.validation_level = r->in.validation_level;
667         r2.in.flags = 0;
668
669         status = netr_LogonSamLogonWithFlags(dce_call, mem_ctx, &r2);
670
671         r->out.return_authenticator = r2.out.return_authenticator;
672         r->out.validation = r2.out.validation;
673         r->out.authoritative = r2.out.authoritative;
674
675         return status;
676 }
677
678
679 /* 
680   netr_LogonSamLogoff 
681 */
682 static NTSTATUS netr_LogonSamLogoff(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
683                        struct netr_LogonSamLogoff *r)
684 {
685         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
686 }
687
688
689
690 /* 
691   netr_DatabaseDeltas 
692 */
693 static NTSTATUS netr_DatabaseDeltas(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
694                        struct netr_DatabaseDeltas *r)
695 {
696         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
697 }
698
699
700 /* 
701   netr_DatabaseSync 
702 */
703 static NTSTATUS netr_DatabaseSync(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
704                        struct netr_DatabaseSync *r)
705 {
706         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
707 }
708
709
710 /* 
711   netr_AccountDeltas 
712 */
713 static NTSTATUS netr_AccountDeltas(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
714                        struct netr_AccountDeltas *r)
715 {
716         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
717 }
718
719
720 /* 
721   netr_AccountSync 
722 */
723 static NTSTATUS netr_AccountSync(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
724                        struct netr_AccountSync *r)
725 {
726         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
727 }
728
729
730 /* 
731   netr_GetDcName 
732 */
733 static NTSTATUS netr_GetDcName(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
734                        struct netr_GetDcName *r)
735 {
736         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
737 }
738
739
740 /* 
741   netr_LogonControl 
742 */
743 static WERROR netr_LogonControl(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
744                        struct netr_LogonControl *r)
745 {
746         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
747 }
748
749
750 /* 
751   netr_GetAnyDCName 
752 */
753 static WERROR netr_GetAnyDCName(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
754                        struct netr_GetAnyDCName *r)
755 {
756         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
757 }
758
759
760 /* 
761   netr_LogonControl2 
762 */
763 static WERROR netr_LogonControl2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
764                        struct netr_LogonControl2 *r)
765 {
766         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
767 }
768
769
770 /* 
771   netr_DatabaseSync2 
772 */
773 static NTSTATUS netr_DatabaseSync2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
774                        struct netr_DatabaseSync2 *r)
775 {
776         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
777 }
778
779
780 /* 
781   netr_DatabaseRedo 
782 */
783 static NTSTATUS netr_DatabaseRedo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
784                        struct netr_DatabaseRedo *r)
785 {
786         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
787 }
788
789
790 /* 
791   netr_LogonControl2Ex 
792 */
793 static WERROR netr_LogonControl2Ex(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
794                        struct netr_LogonControl2Ex *r)
795 {
796         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
797 }
798
799
800 /* 
801   netr_NETRENUMERATETRUSTEDDOMAINS 
802 */
803 static WERROR netr_NETRENUMERATETRUSTEDDOMAINS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
804                        struct netr_NETRENUMERATETRUSTEDDOMAINS *r)
805 {
806         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
807 }
808
809
810 /* 
811   netr_DSRGETDCNAME 
812 */
813 static WERROR netr_DSRGETDCNAME(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
814                        struct netr_DSRGETDCNAME *r)
815 {
816         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
817 }
818
819
820 /* 
821   netr_NETRLOGONDUMMYROUTINE1 
822 */
823 static WERROR netr_NETRLOGONDUMMYROUTINE1(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
824                        struct netr_NETRLOGONDUMMYROUTINE1 *r)
825 {
826         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
827 }
828
829
830 /* 
831   netr_NETRLOGONSETSERVICEBITS 
832 */
833 static WERROR netr_NETRLOGONSETSERVICEBITS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
834                        struct netr_NETRLOGONSETSERVICEBITS *r)
835 {
836         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
837 }
838
839
840 /* 
841   netr_NETRLOGONGETTRUSTRID 
842 */
843 static WERROR netr_NETRLOGONGETTRUSTRID(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
844                        struct netr_NETRLOGONGETTRUSTRID *r)
845 {
846         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
847 }
848
849
850 /* 
851   netr_NETRLOGONCOMPUTESERVERDIGEST 
852 */
853 static WERROR netr_NETRLOGONCOMPUTESERVERDIGEST(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
854                        struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
855 {
856         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
857 }
858
859
860 /* 
861   netr_NETRLOGONCOMPUTECLIENTDIGEST 
862 */
863 static WERROR netr_NETRLOGONCOMPUTECLIENTDIGEST(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
864                        struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
865 {
866         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
867 }
868
869
870 /* 
871   netr_DSRGETDCNAMEX 
872 */
873 static WERROR netr_DSRGETDCNAMEX(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
874                        struct netr_DSRGETDCNAMEX *r)
875 {
876         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
877 }
878
879
880 /* 
881   netr_DSRGETSITENAME 
882 */
883 static WERROR netr_DSRGETSITENAME(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
884                        struct netr_DSRGETSITENAME *r)
885 {
886         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
887 }
888
889
890 /*
891   fill in a netr_DomainTrustInfo from a ldb search result
892 */
893 static NTSTATUS fill_domain_trust_info(TALLOC_CTX *mem_ctx, struct ldb_message *res,
894                                        struct netr_DomainTrustInfo *info)
895 {
896         ZERO_STRUCTP(info);
897         
898         info->domainname.string = samdb_result_string(res, "flatName", NULL);
899         if (info->domainname.string == NULL) {
900                 info->domainname.string = samdb_result_string(res, "name", NULL);
901                 info->fulldomainname.string = samdb_result_string(res, "dnsDomain", NULL);
902         } else {
903                 info->fulldomainname.string = samdb_result_string(res, "name", NULL);
904         }
905
906         /* TODO: we need proper forest support */
907         info->forest.string = info->fulldomainname.string;
908
909         info->guid = samdb_result_guid(res, "objectGUID");
910         info->sid = samdb_result_dom_sid(mem_ctx, res, "objectSid");
911
912         return NT_STATUS_OK;
913 }
914
915 /* 
916   netr_LogonGetDomainInfo
917   this is called as part of the ADS domain logon procedure.
918 */
919 static NTSTATUS netr_LogonGetDomainInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
920                                         struct netr_LogonGetDomainInfo *r)
921 {
922         struct server_pipe_state *pipe_state = dce_call->conn->private;
923         const char * const attrs[] = { "name", "dnsDomain", "objectSid", 
924                                        "objectGUID", "flatName", NULL };
925         void *sam_ctx;
926         struct ldb_message **res1, **res2;
927         struct netr_DomainInfo1 *info1;
928         int ret1, ret2, i;
929         NTSTATUS status;
930
931         if (!pipe_state) {
932                 return NT_STATUS_ACCESS_DENIED;
933         }
934
935         if (!netr_creds_server_step_check(pipe_state, 
936                                           r->in.credential, r->out.credential)) {
937                 return NT_STATUS_ACCESS_DENIED;
938         }
939
940         sam_ctx = samdb_connect(mem_ctx);
941         if (sam_ctx == NULL) {
942                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
943         }
944
945         /* we need to do two searches. The first will pull our primary
946            domain and the second will pull any trusted domains. Our
947            primary domain is also a "trusted" domain, so we need to
948            put the primary domain into the lists of returned trusts as
949            well */
950         ret1 = samdb_search(sam_ctx, mem_ctx, NULL, &res1, attrs, "(objectClass=domainDNS)");
951         if (ret1 != 1) {
952                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
953         }
954
955         ret2 = samdb_search(sam_ctx, mem_ctx, NULL, &res2, attrs, "(objectClass=trustedDomain)");
956         if (ret2 == -1) {
957                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
958         }
959
960         info1 = talloc_p(mem_ctx, struct netr_DomainInfo1);
961         if (info1 == NULL) {
962                 return NT_STATUS_NO_MEMORY;
963         }
964
965         ZERO_STRUCTP(info1);
966
967         info1->num_trusts = ret2 + 1;
968         info1->trusts = talloc_array_p(mem_ctx, struct netr_DomainTrustInfo, 
969                                        info1->num_trusts);
970         if (info1->trusts == NULL) {
971                 return NT_STATUS_NO_MEMORY;
972         }
973
974         status = fill_domain_trust_info(mem_ctx, res1[0], &info1->domaininfo);
975         if (!NT_STATUS_IS_OK(status)) {
976                 return status;
977         }
978
979         status = fill_domain_trust_info(mem_ctx, res1[0], &info1->trusts[0]);
980         if (!NT_STATUS_IS_OK(status)) {
981                 return status;
982         }
983
984         for (i=0;i<ret2;i++) {
985                 status = fill_domain_trust_info(mem_ctx, res2[i], &info1->trusts[i+1]);
986                 if (!NT_STATUS_IS_OK(status)) {
987                         return status;
988                 }
989         }
990
991         r->out.info.info1 = info1;
992
993         return NT_STATUS_OK;
994 }
995
996
997 /* 
998   netr_NETRSERVERPASSWORDSET2 
999 */
1000 static WERROR netr_NETRSERVERPASSWORDSET2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1001                        struct netr_NETRSERVERPASSWORDSET2 *r)
1002 {
1003         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1004 }
1005
1006
1007 /* 
1008   netr_NETRSERVERPASSWORDGET 
1009 */
1010 static WERROR netr_NETRSERVERPASSWORDGET(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1011                        struct netr_NETRSERVERPASSWORDGET *r)
1012 {
1013         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1014 }
1015
1016
1017 /* 
1018   netr_NETRLOGONSENDTOSAM 
1019 */
1020 static WERROR netr_NETRLOGONSENDTOSAM(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1021                        struct netr_NETRLOGONSENDTOSAM *r)
1022 {
1023         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1024 }
1025
1026
1027 /* 
1028   netr_DSRADDRESSTOSITENAMESW 
1029 */
1030 static WERROR netr_DSRADDRESSTOSITENAMESW(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1031                        struct netr_DSRADDRESSTOSITENAMESW *r)
1032 {
1033         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1034 }
1035
1036
1037 /* 
1038   netr_DSRGETDCNAMEEX2 
1039 */
1040 static WERROR netr_DSRGETDCNAMEEX2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1041                        struct netr_DSRGETDCNAMEEX2 *r)
1042 {
1043         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1044 }
1045
1046
1047 /* 
1048   netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN 
1049 */
1050 static WERROR netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1051                        struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
1052 {
1053         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1054 }
1055
1056
1057 /* 
1058   netr_NETRENUMERATETRUSTEDDOMAINSEX 
1059 */
1060 static WERROR netr_NETRENUMERATETRUSTEDDOMAINSEX(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1061                        struct netr_NETRENUMERATETRUSTEDDOMAINSEX *r)
1062 {
1063         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1064 }
1065
1066
1067 /* 
1068   netr_DSRADDRESSTOSITENAMESEXW 
1069 */
1070 static WERROR netr_DSRADDRESSTOSITENAMESEXW(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1071                        struct netr_DSRADDRESSTOSITENAMESEXW *r)
1072 {
1073         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1074 }
1075
1076
1077 /* 
1078   netr_DSRGETDCSITECOVERAGEW 
1079 */
1080 static WERROR netr_DSRGETDCSITECOVERAGEW(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1081                        struct netr_DSRGETDCSITECOVERAGEW *r)
1082 {
1083         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1084 }
1085
1086
1087 /* 
1088   netr_NETRLOGONSAMLOGONEX 
1089 */
1090 static WERROR netr_NETRLOGONSAMLOGONEX(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1091                        struct netr_NETRLOGONSAMLOGONEX *r)
1092 {
1093         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1094 }
1095
1096
1097 /* 
1098   netr_DsrEnumerateDomainTrusts 
1099 */
1100 static WERROR netr_DsrEnumerateDomainTrusts(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1101                                               struct netr_DsrEnumerateDomainTrusts *r)
1102 {
1103         struct netr_DomainTrust *trusts;
1104         void *sam_ctx;
1105         int ret, i;
1106         struct ldb_message **res;
1107         const char * const attrs[] = { "name", "dnsDomain", "objectSid", "objectGUID", NULL };
1108
1109         ZERO_STRUCT(r->out);
1110
1111         sam_ctx = samdb_connect(mem_ctx);
1112         if (sam_ctx == NULL) {
1113                 return WERR_GENERAL_FAILURE;
1114         }
1115
1116         ret = samdb_search(sam_ctx, mem_ctx, NULL, &res, attrs, "(objectClass=domainDNS)");
1117         if (ret == -1) {
1118                 return WERR_GENERAL_FAILURE;            
1119         }
1120
1121         if (ret == 0) {
1122                 return WERR_OK;
1123         }
1124
1125         trusts = talloc_array_p(mem_ctx, struct netr_DomainTrust, ret);
1126         if (trusts == NULL) {
1127                 return WERR_NOMEM;
1128         }
1129         
1130         r->out.count = ret;
1131         r->out.trusts = trusts;
1132
1133         /* TODO: add filtering by trust_flags, and correct trust_type
1134            and attributes */
1135         for (i=0;i<ret;i++) {
1136                 trusts[i].netbios_name = samdb_result_string(res[i], "name", NULL);
1137                 trusts[i].dns_name     = samdb_result_string(res[i], "dnsDomain", NULL);
1138                 trusts[i].trust_flags = 
1139                         NETR_TRUST_FLAG_TREEROOT | 
1140                         NETR_TRUST_FLAG_IN_FOREST | 
1141                         NETR_TRUST_FLAG_PRIMARY;
1142                 trusts[i].parent_index = 0;
1143                 trusts[i].trust_type = 2;
1144                 trusts[i].trust_attributes = 0;
1145                 trusts[i].sid  = samdb_result_dom_sid(mem_ctx, res[i], "objectSid");
1146                 trusts[i].guid = samdb_result_guid(res[i], "objectGUID");
1147         }
1148         
1149
1150         return WERR_OK;
1151 }
1152
1153
1154 /* 
1155   netr_DSRDEREGISTERDNSHOSTRECORDS 
1156 */
1157 static WERROR netr_DSRDEREGISTERDNSHOSTRECORDS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1158                        struct netr_DSRDEREGISTERDNSHOSTRECORDS *r)
1159 {
1160         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1161 }
1162
1163
1164 /* 
1165   netr_NETRSERVERTRUSTPASSWORDSGET 
1166 */
1167 static WERROR netr_NETRSERVERTRUSTPASSWORDSGET(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1168                        struct netr_NETRSERVERTRUSTPASSWORDSGET *r)
1169 {
1170         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1171 }
1172
1173
1174 /* 
1175   netr_DSRGETFORESTTRUSTINFORMATION 
1176 */
1177 static WERROR netr_DSRGETFORESTTRUSTINFORMATION(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1178                        struct netr_DSRGETFORESTTRUSTINFORMATION *r)
1179 {
1180         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1181 }
1182
1183
1184 /* 
1185   netr_NETRGETFORESTTRUSTINFORMATION 
1186 */
1187 static WERROR netr_NETRGETFORESTTRUSTINFORMATION(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1188                        struct netr_NETRGETFORESTTRUSTINFORMATION *r)
1189 {
1190         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1191 }
1192
1193
1194 /* 
1195   netr_NETRSERVERGETTRUSTINFO 
1196 */
1197 static WERROR netr_NETRSERVERGETTRUSTINFO(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1198                        struct netr_NETRSERVERGETTRUSTINFO *r)
1199 {
1200         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1201 }
1202
1203
1204 /* include the generated boilerplate */
1205 #include "librpc/gen_ndr/ndr_netlogon_s.c"