r2648: - use a destructor on struct server_connection to simplify the
[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(pipe_state, &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(pipe_state,
519                                                     &auth_context, r->in.logon.network->challenge);
520                 if (!NT_STATUS_IS_OK(nt_status)) {
521                         return nt_status;
522                 }
523
524                 nt_status = make_user_info_netlogon_network(&user_info,
525                                                             r->in.logon.network->identity_info.account_name.string,
526                                                             r->in.logon.network->identity_info.domain_name.string,
527                                                             r->in.logon.network->identity_info.workstation.string,
528                                                             r->in.logon.network->lm.data, r->in.logon.network->lm.length,
529                                                             r->in.logon.network->nt.data, r->in.logon.network->nt.length);
530                 break;
531         default:
532                 free_auth_context(&auth_context);
533                 return NT_STATUS_INVALID_PARAMETER;
534         }
535         
536         if (!NT_STATUS_IS_OK(nt_status)) {
537                 return nt_status;
538         }
539
540         nt_status = auth_context->check_ntlm_password(auth_context,
541                                                       user_info, 
542                                                       &server_info);
543
544         if (!NT_STATUS_IS_OK(nt_status)) {
545                 free_auth_context(&auth_context);
546                 return nt_status;
547         }
548         free_auth_context(&auth_context);
549
550         sam = talloc_p(mem_ctx, struct netr_SamBaseInfo);
551
552         ZERO_STRUCTP(sam);
553         
554         sam->last_logon = server_info->last_logon;
555         sam->last_logoff = server_info->last_logoff;
556         sam->acct_expiry = server_info->acct_expiry;
557         sam->last_password_change = server_info->last_password_change;
558         sam->allow_password_change = server_info->allow_password_change;
559         sam->force_password_change = server_info->force_password_change;
560         
561         sam->account_name.string = talloc_strdup(mem_ctx, server_info->account_name);
562         sam->full_name.string = talloc_strdup(mem_ctx, server_info->full_name);
563         sam->logon_script.string = talloc_strdup(mem_ctx, server_info->logon_script);
564         sam->profile_path.string = talloc_strdup(mem_ctx, server_info->profile_path);
565         sam->home_directory.string = talloc_strdup(mem_ctx, server_info->home_directory);
566         sam->home_drive.string = talloc_strdup(mem_ctx, server_info->home_drive);
567         
568         sam->logon_count = server_info->logon_count;
569         sam->bad_password_count = sam->bad_password_count;
570         sam->rid = server_info->user_sid->sub_auths[server_info->user_sid->num_auths-1];
571         sam->primary_gid = server_info->primary_group_sid->sub_auths[server_info->primary_group_sid->num_auths-1];
572         sam->group_count = 0;
573         sam->groupids = NULL;
574         sam->user_flags = 0; /* TODO: w2k3 uses 0x120 - what is this? */
575         sam->acct_flags = server_info->acct_flags;      
576         sam->logon_server.string = lp_netbios_name();
577         
578         sam->domain.string = talloc_strdup(mem_ctx, server_info->domain);
579         
580         sam->domain_sid = dom_sid_dup(mem_ctx, server_info->user_sid);
581         sam->domain_sid->num_auths--;
582
583         if (server_info->user_session_key.length == sizeof(sam->key.key)) {
584                 memcpy(sam->key.key, server_info->user_session_key.data, sizeof(sam->key.key));
585         } else {
586                 ZERO_STRUCT(sam->key.key);
587         }
588         
589         /* Don't crypt an all-zero key, it would give away the NETLOGON pipe session key */
590         /* It appears that level 6 is not individually encrypted */
591         if ((r->in.validation_level != 6) 
592             && memcmp(sam->key.key, zeros,  
593                       sizeof(sam->key.key)) != 0) {
594                 creds_arcfour_crypt(pipe_state->creds, 
595                                     sam->key.key, 
596                                     sizeof(sam->key.key));
597         }
598         
599         if (server_info->lm_session_key.length == sizeof(sam->LMSessKey.key)) {
600                 memcpy(sam->LMSessKey.key, server_info->lm_session_key.data, 
601                        sizeof(sam->LMSessKey.key));
602         } else {
603                 ZERO_STRUCT(sam->LMSessKey.key);
604         }
605         
606         /* Don't crypt an all-zero key, it would give away the NETLOGON pipe session key */
607         /* It appears that level 6 is not individually encrypted */
608         if ((r->in.validation_level != 6) 
609             && memcmp(sam->LMSessKey.key, zeros,  
610                       sizeof(sam->LMSessKey.key)) != 0) {
611                 creds_arcfour_crypt(pipe_state->creds, 
612                                     sam->LMSessKey.key, 
613                                     sizeof(sam->LMSessKey.key));
614         }
615
616         switch (r->in.validation_level) {
617         case 2:
618                 sam2 = talloc_p(mem_ctx, struct netr_SamInfo2);
619                 ZERO_STRUCTP(sam2);
620                 sam2->base = *sam;
621                 r->out.validation.sam2 = sam2;
622                 break;
623
624         case 3:
625                 sam3 = talloc_p(mem_ctx, struct netr_SamInfo3);
626                 ZERO_STRUCTP(sam3);
627                 sam3->base = *sam;
628                 r->out.validation.sam3 = sam3;
629                 break;
630
631         case 6:
632                 sam6 = talloc_p(mem_ctx, struct netr_SamInfo6);
633                 ZERO_STRUCTP(sam6);
634                 sam6->base = *sam;
635                 sam6->forest.string = lp_realm();
636                 sam6->principle.string = talloc_asprintf(mem_ctx, "%s@%s", 
637                                                          sam->account_name.string, sam6->forest.string);
638                 r->out.validation.sam6 = sam6;
639                 break;
640
641         default:
642                 break;
643         }
644
645         r->out.authoritative = 1;
646
647         return NT_STATUS_OK;
648 }
649
650 /* 
651   netr_LogonSamLogon
652 */
653 static NTSTATUS netr_LogonSamLogon(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
654                                    struct netr_LogonSamLogon *r)
655 {
656         struct netr_LogonSamLogonWithFlags r2;
657         NTSTATUS status;
658
659         ZERO_STRUCT(r2);
660
661         r2.in.server_name = r->in.server_name;
662         r2.in.workstation = r->in.workstation;
663         r2.in.credential  = r->in.credential;
664         r2.in.return_authenticator = r->in.return_authenticator;
665         r2.in.logon_level = r->in.logon_level;
666         r2.in.logon = r->in.logon;
667         r2.in.validation_level = r->in.validation_level;
668         r2.in.flags = 0;
669
670         status = netr_LogonSamLogonWithFlags(dce_call, mem_ctx, &r2);
671
672         r->out.return_authenticator = r2.out.return_authenticator;
673         r->out.validation = r2.out.validation;
674         r->out.authoritative = r2.out.authoritative;
675
676         return status;
677 }
678
679
680 /* 
681   netr_LogonSamLogoff 
682 */
683 static NTSTATUS netr_LogonSamLogoff(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
684                        struct netr_LogonSamLogoff *r)
685 {
686         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
687 }
688
689
690
691 /* 
692   netr_DatabaseDeltas 
693 */
694 static NTSTATUS netr_DatabaseDeltas(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
695                        struct netr_DatabaseDeltas *r)
696 {
697         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
698 }
699
700
701 /* 
702   netr_DatabaseSync 
703 */
704 static NTSTATUS netr_DatabaseSync(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
705                        struct netr_DatabaseSync *r)
706 {
707         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
708 }
709
710
711 /* 
712   netr_AccountDeltas 
713 */
714 static NTSTATUS netr_AccountDeltas(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
715                        struct netr_AccountDeltas *r)
716 {
717         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
718 }
719
720
721 /* 
722   netr_AccountSync 
723 */
724 static NTSTATUS netr_AccountSync(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
725                        struct netr_AccountSync *r)
726 {
727         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
728 }
729
730
731 /* 
732   netr_GetDcName 
733 */
734 static NTSTATUS netr_GetDcName(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
735                        struct netr_GetDcName *r)
736 {
737         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
738 }
739
740
741 /* 
742   netr_LogonControl 
743 */
744 static WERROR netr_LogonControl(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
745                        struct netr_LogonControl *r)
746 {
747         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
748 }
749
750
751 /* 
752   netr_GetAnyDCName 
753 */
754 static WERROR netr_GetAnyDCName(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
755                        struct netr_GetAnyDCName *r)
756 {
757         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
758 }
759
760
761 /* 
762   netr_LogonControl2 
763 */
764 static WERROR netr_LogonControl2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
765                        struct netr_LogonControl2 *r)
766 {
767         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
768 }
769
770
771 /* 
772   netr_DatabaseSync2 
773 */
774 static NTSTATUS netr_DatabaseSync2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
775                        struct netr_DatabaseSync2 *r)
776 {
777         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
778 }
779
780
781 /* 
782   netr_DatabaseRedo 
783 */
784 static NTSTATUS netr_DatabaseRedo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
785                        struct netr_DatabaseRedo *r)
786 {
787         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
788 }
789
790
791 /* 
792   netr_LogonControl2Ex 
793 */
794 static WERROR netr_LogonControl2Ex(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
795                        struct netr_LogonControl2Ex *r)
796 {
797         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
798 }
799
800
801 /* 
802   netr_NETRENUMERATETRUSTEDDOMAINS 
803 */
804 static WERROR netr_NETRENUMERATETRUSTEDDOMAINS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
805                        struct netr_NETRENUMERATETRUSTEDDOMAINS *r)
806 {
807         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
808 }
809
810
811 /* 
812   netr_DSRGETDCNAME 
813 */
814 static WERROR netr_DSRGETDCNAME(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
815                        struct netr_DSRGETDCNAME *r)
816 {
817         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
818 }
819
820
821 /* 
822   netr_NETRLOGONDUMMYROUTINE1 
823 */
824 static WERROR netr_NETRLOGONDUMMYROUTINE1(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
825                        struct netr_NETRLOGONDUMMYROUTINE1 *r)
826 {
827         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
828 }
829
830
831 /* 
832   netr_NETRLOGONSETSERVICEBITS 
833 */
834 static WERROR netr_NETRLOGONSETSERVICEBITS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
835                        struct netr_NETRLOGONSETSERVICEBITS *r)
836 {
837         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
838 }
839
840
841 /* 
842   netr_NETRLOGONGETTRUSTRID 
843 */
844 static WERROR netr_NETRLOGONGETTRUSTRID(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
845                        struct netr_NETRLOGONGETTRUSTRID *r)
846 {
847         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
848 }
849
850
851 /* 
852   netr_NETRLOGONCOMPUTESERVERDIGEST 
853 */
854 static WERROR netr_NETRLOGONCOMPUTESERVERDIGEST(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
855                        struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
856 {
857         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
858 }
859
860
861 /* 
862   netr_NETRLOGONCOMPUTECLIENTDIGEST 
863 */
864 static WERROR netr_NETRLOGONCOMPUTECLIENTDIGEST(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
865                        struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
866 {
867         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
868 }
869
870
871 /* 
872   netr_DSRGETDCNAMEX 
873 */
874 static WERROR netr_DSRGETDCNAMEX(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
875                        struct netr_DSRGETDCNAMEX *r)
876 {
877         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
878 }
879
880
881 /* 
882   netr_DSRGETSITENAME 
883 */
884 static WERROR netr_DSRGETSITENAME(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
885                        struct netr_DSRGETSITENAME *r)
886 {
887         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
888 }
889
890
891 /*
892   fill in a netr_DomainTrustInfo from a ldb search result
893 */
894 static NTSTATUS fill_domain_trust_info(TALLOC_CTX *mem_ctx, struct ldb_message *res,
895                                        struct netr_DomainTrustInfo *info)
896 {
897         ZERO_STRUCTP(info);
898         
899         info->domainname.string = samdb_result_string(res, "flatName", NULL);
900         if (info->domainname.string == NULL) {
901                 info->domainname.string = samdb_result_string(res, "name", NULL);
902                 info->fulldomainname.string = samdb_result_string(res, "dnsDomain", NULL);
903         } else {
904                 info->fulldomainname.string = samdb_result_string(res, "name", NULL);
905         }
906
907         /* TODO: we need proper forest support */
908         info->forest.string = info->fulldomainname.string;
909
910         info->guid = samdb_result_guid(res, "objectGUID");
911         info->sid = samdb_result_dom_sid(mem_ctx, res, "objectSid");
912
913         return NT_STATUS_OK;
914 }
915
916 /* 
917   netr_LogonGetDomainInfo
918   this is called as part of the ADS domain logon procedure.
919 */
920 static NTSTATUS netr_LogonGetDomainInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
921                                         struct netr_LogonGetDomainInfo *r)
922 {
923         struct server_pipe_state *pipe_state = dce_call->conn->private;
924         const char * const attrs[] = { "name", "dnsDomain", "objectSid", 
925                                        "objectGUID", "flatName", NULL };
926         void *sam_ctx;
927         struct ldb_message **res1, **res2;
928         struct netr_DomainInfo1 *info1;
929         int ret1, ret2, i;
930         NTSTATUS status;
931
932         if (!pipe_state) {
933                 return NT_STATUS_ACCESS_DENIED;
934         }
935
936         if (!netr_creds_server_step_check(pipe_state, 
937                                           r->in.credential, r->out.credential)) {
938                 return NT_STATUS_ACCESS_DENIED;
939         }
940
941         sam_ctx = samdb_connect(mem_ctx);
942         if (sam_ctx == NULL) {
943                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
944         }
945
946         /* we need to do two searches. The first will pull our primary
947            domain and the second will pull any trusted domains. Our
948            primary domain is also a "trusted" domain, so we need to
949            put the primary domain into the lists of returned trusts as
950            well */
951         ret1 = samdb_search(sam_ctx, mem_ctx, NULL, &res1, attrs, "(objectClass=domainDNS)");
952         if (ret1 != 1) {
953                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
954         }
955
956         ret2 = samdb_search(sam_ctx, mem_ctx, NULL, &res2, attrs, "(objectClass=trustedDomain)");
957         if (ret2 == -1) {
958                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
959         }
960
961         info1 = talloc_p(mem_ctx, struct netr_DomainInfo1);
962         if (info1 == NULL) {
963                 return NT_STATUS_NO_MEMORY;
964         }
965
966         ZERO_STRUCTP(info1);
967
968         info1->num_trusts = ret2 + 1;
969         info1->trusts = talloc_array_p(mem_ctx, struct netr_DomainTrustInfo, 
970                                        info1->num_trusts);
971         if (info1->trusts == NULL) {
972                 return NT_STATUS_NO_MEMORY;
973         }
974
975         status = fill_domain_trust_info(mem_ctx, res1[0], &info1->domaininfo);
976         if (!NT_STATUS_IS_OK(status)) {
977                 return status;
978         }
979
980         status = fill_domain_trust_info(mem_ctx, res1[0], &info1->trusts[0]);
981         if (!NT_STATUS_IS_OK(status)) {
982                 return status;
983         }
984
985         for (i=0;i<ret2;i++) {
986                 status = fill_domain_trust_info(mem_ctx, res2[i], &info1->trusts[i+1]);
987                 if (!NT_STATUS_IS_OK(status)) {
988                         return status;
989                 }
990         }
991
992         r->out.info.info1 = info1;
993
994         return NT_STATUS_OK;
995 }
996
997
998 /* 
999   netr_NETRSERVERPASSWORDSET2 
1000 */
1001 static WERROR netr_NETRSERVERPASSWORDSET2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1002                        struct netr_NETRSERVERPASSWORDSET2 *r)
1003 {
1004         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1005 }
1006
1007
1008 /* 
1009   netr_NETRSERVERPASSWORDGET 
1010 */
1011 static WERROR netr_NETRSERVERPASSWORDGET(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1012                        struct netr_NETRSERVERPASSWORDGET *r)
1013 {
1014         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1015 }
1016
1017
1018 /* 
1019   netr_NETRLOGONSENDTOSAM 
1020 */
1021 static WERROR netr_NETRLOGONSENDTOSAM(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1022                        struct netr_NETRLOGONSENDTOSAM *r)
1023 {
1024         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1025 }
1026
1027
1028 /* 
1029   netr_DSRADDRESSTOSITENAMESW 
1030 */
1031 static WERROR netr_DSRADDRESSTOSITENAMESW(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1032                        struct netr_DSRADDRESSTOSITENAMESW *r)
1033 {
1034         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1035 }
1036
1037
1038 /* 
1039   netr_DSRGETDCNAMEEX2 
1040 */
1041 static WERROR netr_DSRGETDCNAMEEX2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1042                        struct netr_DSRGETDCNAMEEX2 *r)
1043 {
1044         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1045 }
1046
1047
1048 /* 
1049   netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN 
1050 */
1051 static WERROR netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1052                        struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
1053 {
1054         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1055 }
1056
1057
1058 /* 
1059   netr_NETRENUMERATETRUSTEDDOMAINSEX 
1060 */
1061 static WERROR netr_NETRENUMERATETRUSTEDDOMAINSEX(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1062                        struct netr_NETRENUMERATETRUSTEDDOMAINSEX *r)
1063 {
1064         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1065 }
1066
1067
1068 /* 
1069   netr_DSRADDRESSTOSITENAMESEXW 
1070 */
1071 static WERROR netr_DSRADDRESSTOSITENAMESEXW(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1072                        struct netr_DSRADDRESSTOSITENAMESEXW *r)
1073 {
1074         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1075 }
1076
1077
1078 /* 
1079   netr_DSRGETDCSITECOVERAGEW 
1080 */
1081 static WERROR netr_DSRGETDCSITECOVERAGEW(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1082                        struct netr_DSRGETDCSITECOVERAGEW *r)
1083 {
1084         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1085 }
1086
1087
1088 /* 
1089   netr_NETRLOGONSAMLOGONEX 
1090 */
1091 static WERROR netr_NETRLOGONSAMLOGONEX(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1092                        struct netr_NETRLOGONSAMLOGONEX *r)
1093 {
1094         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1095 }
1096
1097
1098 /* 
1099   netr_DsrEnumerateDomainTrusts 
1100 */
1101 static WERROR netr_DsrEnumerateDomainTrusts(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1102                                               struct netr_DsrEnumerateDomainTrusts *r)
1103 {
1104         struct netr_DomainTrust *trusts;
1105         void *sam_ctx;
1106         int ret, i;
1107         struct ldb_message **res;
1108         const char * const attrs[] = { "name", "dnsDomain", "objectSid", "objectGUID", NULL };
1109
1110         ZERO_STRUCT(r->out);
1111
1112         sam_ctx = samdb_connect(mem_ctx);
1113         if (sam_ctx == NULL) {
1114                 return WERR_GENERAL_FAILURE;
1115         }
1116
1117         ret = samdb_search(sam_ctx, mem_ctx, NULL, &res, attrs, "(objectClass=domainDNS)");
1118         if (ret == -1) {
1119                 return WERR_GENERAL_FAILURE;            
1120         }
1121
1122         if (ret == 0) {
1123                 return WERR_OK;
1124         }
1125
1126         trusts = talloc_array_p(mem_ctx, struct netr_DomainTrust, ret);
1127         if (trusts == NULL) {
1128                 return WERR_NOMEM;
1129         }
1130         
1131         r->out.count = ret;
1132         r->out.trusts = trusts;
1133
1134         /* TODO: add filtering by trust_flags, and correct trust_type
1135            and attributes */
1136         for (i=0;i<ret;i++) {
1137                 trusts[i].netbios_name = samdb_result_string(res[i], "name", NULL);
1138                 trusts[i].dns_name     = samdb_result_string(res[i], "dnsDomain", NULL);
1139                 trusts[i].trust_flags = 
1140                         NETR_TRUST_FLAG_TREEROOT | 
1141                         NETR_TRUST_FLAG_IN_FOREST | 
1142                         NETR_TRUST_FLAG_PRIMARY;
1143                 trusts[i].parent_index = 0;
1144                 trusts[i].trust_type = 2;
1145                 trusts[i].trust_attributes = 0;
1146                 trusts[i].sid  = samdb_result_dom_sid(mem_ctx, res[i], "objectSid");
1147                 trusts[i].guid = samdb_result_guid(res[i], "objectGUID");
1148         }
1149         
1150
1151         return WERR_OK;
1152 }
1153
1154
1155 /* 
1156   netr_DSRDEREGISTERDNSHOSTRECORDS 
1157 */
1158 static WERROR netr_DSRDEREGISTERDNSHOSTRECORDS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1159                        struct netr_DSRDEREGISTERDNSHOSTRECORDS *r)
1160 {
1161         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1162 }
1163
1164
1165 /* 
1166   netr_NETRSERVERTRUSTPASSWORDSGET 
1167 */
1168 static WERROR netr_NETRSERVERTRUSTPASSWORDSGET(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1169                        struct netr_NETRSERVERTRUSTPASSWORDSGET *r)
1170 {
1171         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1172 }
1173
1174
1175 /* 
1176   netr_DSRGETFORESTTRUSTINFORMATION 
1177 */
1178 static WERROR netr_DSRGETFORESTTRUSTINFORMATION(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1179                        struct netr_DSRGETFORESTTRUSTINFORMATION *r)
1180 {
1181         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1182 }
1183
1184
1185 /* 
1186   netr_NETRGETFORESTTRUSTINFORMATION 
1187 */
1188 static WERROR netr_NETRGETFORESTTRUSTINFORMATION(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1189                        struct netr_NETRGETFORESTTRUSTINFORMATION *r)
1190 {
1191         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1192 }
1193
1194
1195 /* 
1196   netr_NETRSERVERGETTRUSTINFO 
1197 */
1198 static WERROR netr_NETRSERVERGETTRUSTINFO(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
1199                        struct netr_NETRSERVERGETTRUSTINFO *r)
1200 {
1201         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
1202 }
1203
1204
1205 /* include the generated boilerplate */
1206 #include "librpc/gen_ndr/ndr_netlogon_s.c"