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