d5fd745711a867d5bbfa1489bd9e06adc3a716d9
[nivanova/samba-autobuild/.git] / source3 / rpc_server / netlogon / srv_netlog_nt.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-1997,
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6  *  Copyright (C) Paul Ashton                       1997.
7  *  Copyright (C) Jeremy Allison               1998-2001.
8  *  Copyright (C) Andrew Bartlett                   2001.
9  *  Copyright (C) Guenther Deschner                 2008-2009.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 3 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
23  */
24
25 /* This is the implementation of the netlogon pipe. */
26
27 #include "includes.h"
28 #include "system/passwd.h" /* uid_wrapper */
29 #include "ntdomain.h"
30 #include "../libcli/auth/schannel.h"
31 #include "../librpc/gen_ndr/srv_netlogon.h"
32 #include "../librpc/gen_ndr/ndr_samr_c.h"
33 #include "../librpc/gen_ndr/ndr_lsa_c.h"
34 #include "rpc_client/cli_lsarpc.h"
35 #include "rpc_client/init_lsa.h"
36 #include "rpc_server/rpc_ncacn_np.h"
37 #include "../libcli/security/security.h"
38 #include "../libcli/security/dom_sid.h"
39 #include "librpc/gen_ndr/ndr_drsblobs.h"
40 #include "lib/crypto/arcfour.h"
41 #include "lib/crypto/md4.h"
42 #include "nsswitch/libwbclient/wbclient.h"
43 #include "../libcli/registry/util_reg.h"
44 #include "passdb.h"
45 #include "auth.h"
46 #include "messages.h"
47 #include "../lib/tsocket/tsocket.h"
48 #include "lib/param/param.h"
49
50 extern userdom_struct current_user_info;
51
52 #undef DBGC_CLASS
53 #define DBGC_CLASS DBGC_RPC_SRV
54
55 struct netlogon_server_pipe_state {
56         struct netr_Credential client_challenge;
57         struct netr_Credential server_challenge;
58 };
59
60 /*************************************************************************
61  _netr_LogonControl
62  *************************************************************************/
63
64 WERROR _netr_LogonControl(struct pipes_struct *p,
65                           struct netr_LogonControl *r)
66 {
67         struct netr_LogonControl2Ex l;
68
69         switch (r->in.level) {
70         case 1:
71                 break;
72         case 2:
73                 return WERR_NOT_SUPPORTED;
74         default:
75                 return WERR_UNKNOWN_LEVEL;
76         }
77
78         switch (r->in.function_code) {
79         case NETLOGON_CONTROL_QUERY:
80         case NETLOGON_CONTROL_REPLICATE:
81         case NETLOGON_CONTROL_SYNCHRONIZE:
82         case NETLOGON_CONTROL_PDC_REPLICATE:
83         case NETLOGON_CONTROL_BREAKPOINT:
84         case NETLOGON_CONTROL_BACKUP_CHANGE_LOG:
85         case NETLOGON_CONTROL_TRUNCATE_LOG:
86                 break;
87         default:
88                 return WERR_NOT_SUPPORTED;
89         }
90
91         l.in.logon_server       = r->in.logon_server;
92         l.in.function_code      = r->in.function_code;
93         l.in.level              = r->in.level;
94         l.in.data               = NULL;
95         l.out.query             = r->out.query;
96
97         return _netr_LogonControl2Ex(p, &l);
98 }
99
100 /*************************************************************************
101  _netr_LogonControl2
102  *************************************************************************/
103
104 WERROR _netr_LogonControl2(struct pipes_struct *p,
105                            struct netr_LogonControl2 *r)
106 {
107         struct netr_LogonControl2Ex l;
108
109         l.in.logon_server       = r->in.logon_server;
110         l.in.function_code      = r->in.function_code;
111         l.in.level              = r->in.level;
112         l.in.data               = r->in.data;
113         l.out.query             = r->out.query;
114
115         return _netr_LogonControl2Ex(p, &l);
116 }
117
118 /*************************************************************************
119  *************************************************************************/
120
121 static bool wb_change_trust_creds(const char *domain, WERROR *tc_status)
122 {
123         wbcErr result;
124         struct wbcAuthErrorInfo *error = NULL;
125
126         result = wbcChangeTrustCredentials(domain, &error);
127         switch (result) {
128         case WBC_ERR_WINBIND_NOT_AVAILABLE:
129                 return false;
130         case WBC_ERR_DOMAIN_NOT_FOUND:
131                 *tc_status = WERR_NO_SUCH_DOMAIN;
132                 return true;
133         case WBC_ERR_SUCCESS:
134                 *tc_status = WERR_OK;
135                 return true;
136         default:
137                 break;
138         }
139
140         if (error && error->nt_status != 0) {
141                 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
142         } else {
143                 *tc_status = WERR_TRUST_FAILURE;
144         }
145         wbcFreeMemory(error);
146         return true;
147 }
148
149 /*************************************************************************
150  *************************************************************************/
151
152 static bool wb_check_trust_creds(const char *domain, WERROR *tc_status)
153 {
154         wbcErr result;
155         struct wbcAuthErrorInfo *error = NULL;
156
157         result = wbcCheckTrustCredentials(domain, &error);
158         switch (result) {
159         case WBC_ERR_WINBIND_NOT_AVAILABLE:
160                 return false;
161         case WBC_ERR_DOMAIN_NOT_FOUND:
162                 *tc_status = WERR_NO_SUCH_DOMAIN;
163                 return true;
164         case WBC_ERR_SUCCESS:
165                 *tc_status = WERR_OK;
166                 return true;
167         default:
168                 break;
169         }
170
171         if (error && error->nt_status != 0) {
172                 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
173         } else {
174                 *tc_status = WERR_TRUST_FAILURE;
175         }
176         wbcFreeMemory(error);
177         return true;
178 }
179
180 /****************************************************************
181  _netr_LogonControl2Ex
182 ****************************************************************/
183
184 WERROR _netr_LogonControl2Ex(struct pipes_struct *p,
185                              struct netr_LogonControl2Ex *r)
186 {
187         uint32_t flags = 0x0;
188         WERROR pdc_connection_status = WERR_OK;
189         uint32_t logon_attempts = 0x0;
190         WERROR tc_status;
191         fstring dc_name2;
192         const char *dc_name = NULL;
193         struct sockaddr_storage dc_ss;
194         const char *domain = NULL;
195         struct netr_NETLOGON_INFO_1 *info1;
196         struct netr_NETLOGON_INFO_2 *info2;
197         struct netr_NETLOGON_INFO_3 *info3;
198         struct netr_NETLOGON_INFO_4 *info4;
199         const char *fn;
200         NTSTATUS status;
201         struct netr_DsRGetDCNameInfo *dc_info;
202
203         switch (p->opnum) {
204         case NDR_NETR_LOGONCONTROL:
205                 fn = "_netr_LogonControl";
206                 break;
207         case NDR_NETR_LOGONCONTROL2:
208                 fn = "_netr_LogonControl2";
209                 break;
210         case NDR_NETR_LOGONCONTROL2EX:
211                 fn = "_netr_LogonControl2Ex";
212                 break;
213         default:
214                 return WERR_INVALID_PARAM;
215         }
216
217         switch (r->in.level) {
218         case 1:
219         case 2:
220         case 3:
221         case 4:
222                 break;
223         default:
224                 return WERR_INVALID_LEVEL;
225         }
226
227         switch (r->in.function_code) {
228         case NETLOGON_CONTROL_QUERY:
229                 break;
230         default:
231                 if ((geteuid() != sec_initial_uid()) &&
232                     !nt_token_check_domain_rid(p->session_info->security_token, DOMAIN_RID_ADMINS) &&
233                     !nt_token_check_sid(&global_sid_Builtin_Administrators, p->session_info->security_token))
234                 {
235                         return WERR_ACCESS_DENIED;
236                 }
237                 break;
238         }
239
240         tc_status = WERR_NO_SUCH_DOMAIN;
241
242         switch (r->in.function_code) {
243         case NETLOGON_CONTROL_QUERY:
244                 switch (r->in.level) {
245                 case 1:
246                 case 3:
247                         break;
248                 default:
249                         return WERR_INVALID_PARAMETER;
250                 }
251
252                 tc_status = WERR_OK;
253                 break;
254         case NETLOGON_CONTROL_REPLICATE:
255         case NETLOGON_CONTROL_SYNCHRONIZE:
256         case NETLOGON_CONTROL_PDC_REPLICATE:
257         case NETLOGON_CONTROL_BACKUP_CHANGE_LOG:
258         case NETLOGON_CONTROL_BREAKPOINT:
259         case NETLOGON_CONTROL_TRUNCATE_LOG:
260         case NETLOGON_CONTROL_TRANSPORT_NOTIFY:
261         case NETLOGON_CONTROL_FORCE_DNS_REG:
262         case NETLOGON_CONTROL_QUERY_DNS_REG:
263                 return WERR_NOT_SUPPORTED;
264
265         case NETLOGON_CONTROL_FIND_USER:
266                 if (!r->in.data || !r->in.data->user) {
267                         return WERR_NOT_SUPPORTED;
268                 }
269                 break;
270         case NETLOGON_CONTROL_SET_DBFLAG:
271                 if (!r->in.data) {
272                         return WERR_NOT_SUPPORTED;
273                 }
274                 break;
275         case NETLOGON_CONTROL_TC_VERIFY:
276                 if (!r->in.data || !r->in.data->domain) {
277                         return WERR_NOT_SUPPORTED;
278                 }
279
280                 if (!wb_check_trust_creds(r->in.data->domain, &tc_status)) {
281                         return WERR_NOT_SUPPORTED;
282                 }
283                 break;
284         case NETLOGON_CONTROL_TC_QUERY:
285                 if (!r->in.data || !r->in.data->domain) {
286                         return WERR_NOT_SUPPORTED;
287                 }
288
289                 domain = r->in.data->domain;
290
291                 if (!is_trusted_domain(domain)) {
292                         break;
293                 }
294
295                 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
296                         tc_status = WERR_NO_LOGON_SERVERS;
297                         break;
298                 }
299
300                 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
301                 if (!dc_name) {
302                         return WERR_NOMEM;
303                 }
304
305                 tc_status = WERR_OK;
306
307                 break;
308
309         case NETLOGON_CONTROL_REDISCOVER:
310                 if (!r->in.data || !r->in.data->domain) {
311                         return WERR_NOT_SUPPORTED;
312                 }
313
314                 domain = r->in.data->domain;
315
316                 if (!is_trusted_domain(domain)) {
317                         break;
318                 }
319
320                 status = dsgetdcname(p->mem_ctx, p->msg_ctx, domain, NULL, NULL,
321                                      DS_FORCE_REDISCOVERY | DS_RETURN_FLAT_NAME,
322                                      &dc_info);
323                 if (!NT_STATUS_IS_OK(status)) {
324                         tc_status = WERR_NO_LOGON_SERVERS;
325                         break;
326                 }
327
328                 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_info->dc_unc);
329                 if (!dc_name) {
330                         return WERR_NOMEM;
331                 }
332
333                 tc_status = WERR_OK;
334
335                 break;
336
337         case NETLOGON_CONTROL_CHANGE_PASSWORD:
338                 if (!r->in.data || !r->in.data->domain) {
339                         return WERR_NOT_SUPPORTED;
340                 }
341
342                 if (!wb_change_trust_creds(r->in.data->domain, &tc_status)) {
343                         return WERR_NOT_SUPPORTED;
344                 }
345                 break;
346
347         default:
348                 /* no idea what this should be */
349                 DEBUG(0,("%s: unimplemented function level [%d]\n",
350                         fn, r->in.function_code));
351                 return WERR_NOT_SUPPORTED;
352         }
353
354         /* prepare the response */
355
356         switch (r->in.level) {
357         case 1:
358                 info1 = talloc_zero(p->mem_ctx, struct netr_NETLOGON_INFO_1);
359                 W_ERROR_HAVE_NO_MEMORY(info1);
360
361                 info1->flags                    = flags;
362                 info1->pdc_connection_status    = pdc_connection_status;
363
364                 r->out.query->info1 = info1;
365                 break;
366         case 2:
367                 info2 = talloc_zero(p->mem_ctx, struct netr_NETLOGON_INFO_2);
368                 W_ERROR_HAVE_NO_MEMORY(info2);
369
370                 info2->flags                    = flags;
371                 info2->pdc_connection_status    = pdc_connection_status;
372                 info2->trusted_dc_name          = dc_name;
373                 info2->tc_connection_status     = tc_status;
374
375                 r->out.query->info2 = info2;
376                 break;
377         case 3:
378                 info3 = talloc_zero(p->mem_ctx, struct netr_NETLOGON_INFO_3);
379                 W_ERROR_HAVE_NO_MEMORY(info3);
380
381                 info3->flags                    = flags;
382                 info3->logon_attempts           = logon_attempts;
383
384                 r->out.query->info3 = info3;
385                 break;
386         case 4:
387                 info4 = talloc_zero(p->mem_ctx, struct netr_NETLOGON_INFO_4);
388                 W_ERROR_HAVE_NO_MEMORY(info4);
389
390                 info4->trusted_dc_name          = dc_name;
391                 info4->trusted_domain_name      = r->in.data->domain;
392
393                 r->out.query->info4 = info4;
394                 break;
395         default:
396                 return WERR_UNKNOWN_LEVEL;
397         }
398
399         return WERR_OK;
400 }
401
402 /*************************************************************************
403  _netr_NetrEnumerateTrustedDomains
404  *************************************************************************/
405
406 NTSTATUS _netr_NetrEnumerateTrustedDomains(struct pipes_struct *p,
407                                            struct netr_NetrEnumerateTrustedDomains *r)
408 {
409         NTSTATUS status;
410         NTSTATUS result = NT_STATUS_OK;
411         DATA_BLOB blob;
412         size_t num_domains = 0;
413         const char **trusted_domains = NULL;
414         struct lsa_DomainList domain_list;
415         struct dcerpc_binding_handle *h = NULL;
416         struct policy_handle pol;
417         uint32_t enum_ctx = 0;
418         int i;
419         uint32_t max_size = (uint32_t)-1;
420
421         DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
422
423         status = rpcint_binding_handle(p->mem_ctx,
424                                        &ndr_table_lsarpc,
425                                        p->remote_address,
426                                        p->session_info,
427                                        p->msg_ctx,
428                                        &h);
429         if (!NT_STATUS_IS_OK(status)) {
430                 return status;
431         }
432
433         status = dcerpc_lsa_open_policy2(h,
434                                          p->mem_ctx,
435                                          NULL,
436                                          true,
437                                          LSA_POLICY_VIEW_LOCAL_INFORMATION,
438                                          &pol,
439                                          &result);
440         if (!NT_STATUS_IS_OK(status)) {
441                 goto out;
442         }
443         if (!NT_STATUS_IS_OK(result)) {
444                 status = result;
445                 goto out;
446         }
447
448         do {
449                 /* Lookup list of trusted domains */
450                 status = dcerpc_lsa_EnumTrustDom(h,
451                                                  p->mem_ctx,
452                                                  &pol,
453                                                  &enum_ctx,
454                                                  &domain_list,
455                                                  max_size,
456                                                  &result);
457                 if (!NT_STATUS_IS_OK(status)) {
458                         goto out;
459                 }
460                 if (!NT_STATUS_IS_OK(result) &&
461                     !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
462                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
463                         status = result;
464                         goto out;
465                 }
466
467                 for (i = 0; i < domain_list.count; i++) {
468                         if (!add_string_to_array(p->mem_ctx, domain_list.domains[i].name.string,
469                                                  &trusted_domains, &num_domains)) {
470                                 status = NT_STATUS_NO_MEMORY;
471                                 goto out;
472                         }
473                 }
474         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
475
476         if (num_domains > 0) {
477                 /* multi sz terminate */
478                 trusted_domains = talloc_realloc(p->mem_ctx, trusted_domains, const char *, num_domains + 1);
479                 if (trusted_domains == NULL) {
480                         status = NT_STATUS_NO_MEMORY;
481                         goto out;
482                 }
483
484                 trusted_domains[num_domains] = NULL;
485         }
486
487         if (!push_reg_multi_sz(trusted_domains, &blob, trusted_domains)) {
488                 TALLOC_FREE(trusted_domains);
489                 status = NT_STATUS_NO_MEMORY;
490                 goto out;
491         }
492
493         r->out.trusted_domains_blob->data = blob.data;
494         r->out.trusted_domains_blob->length = blob.length;
495
496         DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
497
498         status = NT_STATUS_OK;
499
500  out:
501         if (is_valid_policy_hnd(&pol)) {
502                 dcerpc_lsa_Close(h, p->mem_ctx, &pol, &result);
503         }
504
505         return status;
506 }
507
508 /*************************************************************************
509  *************************************************************************/
510
511 static NTSTATUS samr_find_machine_account(TALLOC_CTX *mem_ctx,
512                                           struct dcerpc_binding_handle *b,
513                                           const char *account_name,
514                                           uint32_t access_mask,
515                                           struct dom_sid2 **domain_sid_p,
516                                           uint32_t *user_rid_p,
517                                           struct policy_handle *user_handle)
518 {
519         NTSTATUS status;
520         NTSTATUS result = NT_STATUS_OK;
521         struct policy_handle connect_handle;
522         struct policy_handle domain_handle = { 0, };
523         struct lsa_String domain_name;
524         struct dom_sid2 *domain_sid;
525         struct lsa_String names;
526         struct samr_Ids rids;
527         struct samr_Ids types;
528         uint32_t rid;
529
530         status = dcerpc_samr_Connect2(b, mem_ctx,
531                                       lp_netbios_name(),
532                                       SAMR_ACCESS_CONNECT_TO_SERVER |
533                                       SAMR_ACCESS_ENUM_DOMAINS |
534                                       SAMR_ACCESS_LOOKUP_DOMAIN,
535                                       &connect_handle,
536                                       &result);
537         if (!NT_STATUS_IS_OK(status)) {
538                 goto out;
539         }
540         if (!NT_STATUS_IS_OK(result)) {
541                 status = result;
542                 goto out;
543         }
544
545         init_lsa_String(&domain_name, get_global_sam_name());
546
547         status = dcerpc_samr_LookupDomain(b, mem_ctx,
548                                           &connect_handle,
549                                           &domain_name,
550                                           &domain_sid,
551                                           &result);
552         if (!NT_STATUS_IS_OK(status)) {
553                 goto out;
554         }
555         if (!NT_STATUS_IS_OK(result)) {
556                 status = result;
557                 goto out;
558         }
559
560         status = dcerpc_samr_OpenDomain(b, mem_ctx,
561                                         &connect_handle,
562                                         SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
563                                         domain_sid,
564                                         &domain_handle,
565                                         &result);
566         if (!NT_STATUS_IS_OK(status)) {
567                 goto out;
568         }
569         if (!NT_STATUS_IS_OK(result)) {
570                 status = result;
571                 goto out;
572         }
573
574         init_lsa_String(&names, account_name);
575
576         status = dcerpc_samr_LookupNames(b, mem_ctx,
577                                          &domain_handle,
578                                          1,
579                                          &names,
580                                          &rids,
581                                          &types,
582                                          &result);
583         if (!NT_STATUS_IS_OK(status)) {
584                 goto out;
585         }
586         if (!NT_STATUS_IS_OK(result)) {
587                 status = result;
588                 goto out;
589         }
590
591         if (rids.count != 1) {
592                 status = NT_STATUS_NO_SUCH_USER;
593                 goto out;
594         }
595         if (types.count != 1) {
596                 status = NT_STATUS_INVALID_PARAMETER;
597                 goto out;
598         }
599         if (types.ids[0] != SID_NAME_USER) {
600                 status = NT_STATUS_NO_SUCH_USER;
601                 goto out;
602         }
603
604         rid = rids.ids[0];
605
606         status = dcerpc_samr_OpenUser(b, mem_ctx,
607                                       &domain_handle,
608                                       access_mask,
609                                       rid,
610                                       user_handle,
611                                       &result);
612         if (!NT_STATUS_IS_OK(status)) {
613                 goto out;
614         }
615         if (!NT_STATUS_IS_OK(result)) {
616                 status = result;
617                 goto out;
618         }
619
620         if (user_rid_p) {
621                 *user_rid_p = rid;
622         }
623
624         if (domain_sid_p) {
625                 *domain_sid_p = domain_sid;
626         }
627
628  out:
629         if (is_valid_policy_hnd(&domain_handle)) {
630                 dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
631         }
632         if (is_valid_policy_hnd(&connect_handle)) {
633                 dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
634         }
635
636         return status;
637 }
638
639 /******************************************************************
640  gets a machine password entry.  checks access rights of the host.
641  ******************************************************************/
642
643 static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
644                           enum netr_SchannelType sec_chan_type,
645                           struct dom_sid *sid,
646                           struct messaging_context *msg_ctx)
647 {
648         NTSTATUS status;
649         NTSTATUS result = NT_STATUS_OK;
650         TALLOC_CTX *mem_ctx;
651         struct dcerpc_binding_handle *h = NULL;
652         struct tsocket_address *local;
653         struct policy_handle user_handle;
654         uint32_t user_rid;
655         struct dom_sid *domain_sid;
656         uint32_t acct_ctrl;
657         union samr_UserInfo *info;
658         struct auth_session_info *session_info;
659         int rc;
660
661 #if 0
662
663     /*
664      * Currently this code is redundant as we already have a filter
665      * by hostname list. What this code really needs to do is to
666      * get a hosts allowed/hosts denied list from the SAM database
667      * on a per user basis, and make the access decision there.
668      * I will leave this code here for now as a reminder to implement
669      * this at a later date. JRA.
670      */
671
672         if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
673                           p->client_id.name,
674                           p->client_id.addr)) {
675                 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
676                 return False;
677         }
678 #endif /* 0 */
679
680         mem_ctx = talloc_stackframe();
681         if (mem_ctx == NULL) {
682                 status = NT_STATUS_NO_MEMORY;
683                 goto out;
684         }
685
686         status = make_session_info_system(mem_ctx, &session_info);
687         if (!NT_STATUS_IS_OK(status)) {
688                 goto out;
689         }
690
691         ZERO_STRUCT(user_handle);
692
693         rc = tsocket_address_inet_from_strings(mem_ctx,
694                                                "ip",
695                                                "127.0.0.1",
696                                                0,
697                                                &local);
698         if (rc < 0) {
699                 status = NT_STATUS_NO_MEMORY;
700                 goto out;
701         }
702
703         status = rpcint_binding_handle(mem_ctx,
704                                        &ndr_table_samr,
705                                        local,
706                                        session_info,
707                                        msg_ctx,
708                                        &h);
709         if (!NT_STATUS_IS_OK(status)) {
710                 goto out;
711         }
712
713         status = samr_find_machine_account(mem_ctx, h, mach_acct,
714                                            SEC_FLAG_MAXIMUM_ALLOWED,
715                                            &domain_sid, &user_rid,
716                                            &user_handle);
717         if (!NT_STATUS_IS_OK(status)) {
718                 goto out;
719         }
720
721         status = dcerpc_samr_QueryUserInfo2(h,
722                                             mem_ctx,
723                                             &user_handle,
724                                             UserControlInformation,
725                                             &info,
726                                             &result);
727         if (!NT_STATUS_IS_OK(status)) {
728                 goto out;
729         }
730         if (!NT_STATUS_IS_OK(result)) {
731                 status = result;
732                 goto out;
733         }
734
735         acct_ctrl = info->info16.acct_flags;
736
737         if (acct_ctrl & ACB_DISABLED) {
738                 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
739                 status = NT_STATUS_ACCOUNT_DISABLED;
740                 goto out;
741         }
742
743         if (!(acct_ctrl & ACB_SVRTRUST) &&
744             !(acct_ctrl & ACB_WSTRUST) &&
745             !(acct_ctrl & ACB_DOMTRUST))
746         {
747                 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
748                 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
749                 goto out;
750         }
751
752         switch (sec_chan_type) {
753                 case SEC_CHAN_BDC:
754                         if (!(acct_ctrl & ACB_SVRTRUST)) {
755                                 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
756                                          "but not a server trust account\n", mach_acct));
757                                 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
758                                 goto out;
759                         }
760                         break;
761                 case SEC_CHAN_WKSTA:
762                         if (!(acct_ctrl & ACB_WSTRUST)) {
763                                 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
764                                          "but not a workstation trust account\n", mach_acct));
765                                 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
766                                 goto out;
767                         }
768                         break;
769                 case SEC_CHAN_DOMAIN:
770                         if (!(acct_ctrl & ACB_DOMTRUST)) {
771                                 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
772                                          "but not a interdomain trust account\n", mach_acct));
773                                 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
774                                 goto out;
775                         }
776                         break;
777                 default:
778                         break;
779         }
780
781         become_root();
782         status = dcerpc_samr_QueryUserInfo2(h,
783                                             mem_ctx,
784                                             &user_handle,
785                                             UserInternal1Information,
786                                             &info,
787                                             &result);
788         unbecome_root();
789         if (!NT_STATUS_IS_OK(status)) {
790                 goto out;
791         }
792         if (!NT_STATUS_IS_OK(result)) {
793                 status = result;
794                 goto out;
795         }
796
797         if (info->info18.nt_pwd_active == 0) {
798                 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
799                 status = NT_STATUS_LOGON_FAILURE;
800                 goto out;
801         }
802
803         /* samr gives out nthash unencrypted (!) */
804         memcpy(md4pw->hash, info->info18.nt_pwd.hash, 16);
805
806         sid_compose(sid, domain_sid, user_rid);
807
808  out:
809         if (h && is_valid_policy_hnd(&user_handle)) {
810                 dcerpc_samr_Close(h, mem_ctx, &user_handle, &result);
811         }
812
813         talloc_free(mem_ctx);
814
815         return status;
816 }
817
818 /*************************************************************************
819  _netr_ServerReqChallenge
820  *************************************************************************/
821
822 NTSTATUS _netr_ServerReqChallenge(struct pipes_struct *p,
823                                   struct netr_ServerReqChallenge *r)
824 {
825         struct netlogon_server_pipe_state *pipe_state =
826                 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
827
828         if (pipe_state) {
829                 DEBUG(10,("_netr_ServerReqChallenge: new challenge requested. Clearing old state.\n"));
830                 talloc_free(pipe_state);
831                 p->private_data = NULL;
832         }
833
834         pipe_state = talloc(p, struct netlogon_server_pipe_state);
835         NT_STATUS_HAVE_NO_MEMORY(pipe_state);
836
837         pipe_state->client_challenge = *r->in.credentials;
838
839         generate_random_buffer(pipe_state->server_challenge.data,
840                                sizeof(pipe_state->server_challenge.data));
841
842         *r->out.return_credentials = pipe_state->server_challenge;
843
844         p->private_data = pipe_state;
845
846         return NT_STATUS_OK;
847 }
848
849 /*************************************************************************
850  _netr_ServerAuthenticate
851  Create the initial credentials.
852  *************************************************************************/
853
854 NTSTATUS _netr_ServerAuthenticate(struct pipes_struct *p,
855                                   struct netr_ServerAuthenticate *r)
856 {
857         struct netr_ServerAuthenticate3 a;
858         uint32_t negotiate_flags = 0;
859         uint32_t rid;
860
861         a.in.server_name                = r->in.server_name;
862         a.in.account_name               = r->in.account_name;
863         a.in.secure_channel_type        = r->in.secure_channel_type;
864         a.in.computer_name              = r->in.computer_name;
865         a.in.credentials                = r->in.credentials;
866         a.in.negotiate_flags            = &negotiate_flags;
867
868         a.out.return_credentials        = r->out.return_credentials;
869         a.out.rid                       = &rid;
870         a.out.negotiate_flags           = &negotiate_flags;
871
872         return _netr_ServerAuthenticate3(p, &a);
873
874 }
875
876 /*************************************************************************
877  _netr_ServerAuthenticate3
878  *************************************************************************/
879
880 NTSTATUS _netr_ServerAuthenticate3(struct pipes_struct *p,
881                                    struct netr_ServerAuthenticate3 *r)
882 {
883         NTSTATUS status;
884         uint32_t srv_flgs;
885         /* r->in.negotiate_flags is an aliased pointer to r->out.negotiate_flags,
886          * so use a copy to avoid destroying the client values. */
887         uint32_t in_neg_flags = *r->in.negotiate_flags;
888         const char *fn;
889         struct loadparm_context *lp_ctx;
890         struct dom_sid sid;
891         struct samr_Password mach_pwd;
892         struct netlogon_creds_CredentialState *creds;
893         struct netlogon_server_pipe_state *pipe_state =
894                 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
895
896         /* According to Microsoft (see bugid #6099)
897          * Windows 7 looks at the negotiate_flags
898          * returned in this structure *even if the
899          * call fails with access denied* ! So in order
900          * to allow Win7 to connect to a Samba NT style
901          * PDC we set the flags before we know if it's
902          * an error or not.
903          */
904
905         /* 0x000001ff */
906         srv_flgs = NETLOGON_NEG_ACCOUNT_LOCKOUT |
907                    NETLOGON_NEG_PERSISTENT_SAMREPL |
908                    NETLOGON_NEG_ARCFOUR |
909                    NETLOGON_NEG_PROMOTION_COUNT |
910                    NETLOGON_NEG_CHANGELOG_BDC |
911                    NETLOGON_NEG_FULL_SYNC_REPL |
912                    NETLOGON_NEG_MULTIPLE_SIDS |
913                    NETLOGON_NEG_REDO |
914                    NETLOGON_NEG_PASSWORD_CHANGE_REFUSAL |
915                    NETLOGON_NEG_PASSWORD_SET2;
916
917         /* Ensure we support strong (128-bit) keys. */
918         if (in_neg_flags & NETLOGON_NEG_STRONG_KEYS) {
919                 srv_flgs |= NETLOGON_NEG_STRONG_KEYS;
920         }
921
922         if (in_neg_flags & NETLOGON_NEG_SUPPORTS_AES) {
923                 srv_flgs |= NETLOGON_NEG_SUPPORTS_AES;
924         }
925
926         if (lp_server_schannel() != false) {
927                 srv_flgs |= NETLOGON_NEG_SCHANNEL;
928         }
929
930         /*
931          * Support authenticaten of trusted domains.
932          *
933          * These flags are the minimum required set which works with win2k3
934          * and win2k8.
935          */
936         if (pdb_capabilities() & PDB_CAP_TRUSTED_DOMAINS_EX) {
937                 srv_flgs |= NETLOGON_NEG_TRANSITIVE_TRUSTS |
938                             NETLOGON_NEG_DNS_DOMAIN_TRUSTS |
939                             NETLOGON_NEG_CROSS_FOREST_TRUSTS |
940                             NETLOGON_NEG_NEUTRALIZE_NT4_EMULATION;
941         }
942
943         switch (p->opnum) {
944                 case NDR_NETR_SERVERAUTHENTICATE:
945                         fn = "_netr_ServerAuthenticate";
946                         break;
947                 case NDR_NETR_SERVERAUTHENTICATE2:
948                         fn = "_netr_ServerAuthenticate2";
949                         break;
950                 case NDR_NETR_SERVERAUTHENTICATE3:
951                         fn = "_netr_ServerAuthenticate3";
952                         break;
953                 default:
954                         return NT_STATUS_INTERNAL_ERROR;
955         }
956
957         /* We use this as the key to store the creds: */
958         /* r->in.computer_name */
959
960         if (!pipe_state) {
961                 DEBUG(0,("%s: no challenge sent to client %s\n", fn,
962                         r->in.computer_name));
963                 status = NT_STATUS_ACCESS_DENIED;
964                 goto out;
965         }
966
967         if ( (lp_server_schannel() == true) &&
968              ((in_neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
969
970                 /* schannel must be used, but client did not offer it. */
971                 DEBUG(0,("%s: schannel required but client failed "
972                         "to offer it. Client was %s\n",
973                         fn, r->in.account_name));
974                 status = NT_STATUS_ACCESS_DENIED;
975                 goto out;
976         }
977
978         status = get_md4pw(&mach_pwd,
979                            r->in.account_name,
980                            r->in.secure_channel_type,
981                            &sid, p->msg_ctx);
982         if (!NT_STATUS_IS_OK(status)) {
983                 DEBUG(0,("%s: failed to get machine password for "
984                         "account %s: %s\n",
985                         fn, r->in.account_name, nt_errstr(status) ));
986                 /* always return NT_STATUS_ACCESS_DENIED */
987                 status = NT_STATUS_ACCESS_DENIED;
988                 goto out;
989         }
990
991         /* From the client / server challenges and md4 password, generate sess key */
992         /* Check client credentials are valid. */
993         creds = netlogon_creds_server_init(p->mem_ctx,
994                                            r->in.account_name,
995                                            r->in.computer_name,
996                                            r->in.secure_channel_type,
997                                            &pipe_state->client_challenge,
998                                            &pipe_state->server_challenge,
999                                            &mach_pwd,
1000                                            r->in.credentials,
1001                                            r->out.return_credentials,
1002                                            srv_flgs);
1003         if (!creds) {
1004                 DEBUG(0,("%s: netlogon_creds_server_check failed. Rejecting auth "
1005                         "request from client %s machine account %s\n",
1006                         fn, r->in.computer_name,
1007                         r->in.account_name));
1008                 status = NT_STATUS_ACCESS_DENIED;
1009                 goto out;
1010         }
1011
1012         creds->sid = dom_sid_dup(creds, &sid);
1013         if (!creds->sid) {
1014                 status = NT_STATUS_NO_MEMORY;
1015                 goto out;
1016         }
1017
1018         lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_helpers());
1019         if (lp_ctx == NULL) {
1020                 DEBUG(10, ("loadparm_init_s3 failed\n"));
1021                 status = NT_STATUS_INTERNAL_ERROR;
1022                 goto out;
1023         }
1024
1025         /* Store off the state so we can continue after client disconnect. */
1026         become_root();
1027         status = schannel_save_creds_state(p->mem_ctx, lp_ctx, creds);
1028         unbecome_root();
1029
1030         talloc_unlink(p->mem_ctx, lp_ctx);
1031
1032         if (!NT_STATUS_IS_OK(status)) {
1033                 ZERO_STRUCTP(r->out.return_credentials);
1034                 goto out;
1035         }
1036
1037         sid_peek_rid(&sid, r->out.rid);
1038
1039         status = NT_STATUS_OK;
1040
1041   out:
1042
1043         *r->out.negotiate_flags = srv_flgs;
1044         return status;
1045 }
1046
1047 /*************************************************************************
1048  _netr_ServerAuthenticate2
1049  *************************************************************************/
1050
1051 NTSTATUS _netr_ServerAuthenticate2(struct pipes_struct *p,
1052                                    struct netr_ServerAuthenticate2 *r)
1053 {
1054         struct netr_ServerAuthenticate3 a;
1055         uint32_t rid;
1056
1057         a.in.server_name                = r->in.server_name;
1058         a.in.account_name               = r->in.account_name;
1059         a.in.secure_channel_type        = r->in.secure_channel_type;
1060         a.in.computer_name              = r->in.computer_name;
1061         a.in.credentials                = r->in.credentials;
1062         a.in.negotiate_flags            = r->in.negotiate_flags;
1063
1064         a.out.return_credentials        = r->out.return_credentials;
1065         a.out.rid                       = &rid;
1066         a.out.negotiate_flags           = r->out.negotiate_flags;
1067
1068         return _netr_ServerAuthenticate3(p, &a);
1069 }
1070
1071 /*************************************************************************
1072  * If schannel is required for this call test that it actually is available.
1073  *************************************************************************/
1074 static NTSTATUS schannel_check_required(struct pipe_auth_data *auth_info,
1075                                         const char *computer_name,
1076                                         bool integrity, bool privacy)
1077 {
1078         if (auth_info && auth_info->auth_type == DCERPC_AUTH_TYPE_SCHANNEL) {
1079                 if (!privacy && !integrity) {
1080                         return NT_STATUS_OK;
1081                 }
1082
1083                 if ((!privacy && integrity) &&
1084                     auth_info->auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
1085                         return NT_STATUS_OK;
1086                 }
1087
1088                 if ((privacy || integrity) &&
1089                     auth_info->auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1090                         return NT_STATUS_OK;
1091                 }
1092         }
1093
1094         /* test didn't pass */
1095         DEBUG(0, ("schannel_check_required: [%s] is not using schannel\n",
1096                   computer_name));
1097
1098         return NT_STATUS_ACCESS_DENIED;
1099 }
1100
1101 /*************************************************************************
1102  *************************************************************************/
1103
1104 static NTSTATUS netr_creds_server_step_check(struct pipes_struct *p,
1105                                              TALLOC_CTX *mem_ctx,
1106                                              const char *computer_name,
1107                                              struct netr_Authenticator *received_authenticator,
1108                                              struct netr_Authenticator *return_authenticator,
1109                                              struct netlogon_creds_CredentialState **creds_out)
1110 {
1111         NTSTATUS status;
1112         bool schannel_global_required = (lp_server_schannel() == true) ? true:false;
1113         struct loadparm_context *lp_ctx;
1114
1115         if (creds_out != NULL) {
1116                 *creds_out = NULL;
1117         }
1118
1119         if (schannel_global_required) {
1120                 status = schannel_check_required(&p->auth,
1121                                                  computer_name,
1122                                                  false, false);
1123                 if (!NT_STATUS_IS_OK(status)) {
1124                         return status;
1125                 }
1126         }
1127
1128         lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
1129         if (lp_ctx == NULL) {
1130                 DEBUG(0, ("loadparm_init_s3 failed\n"));
1131                 return NT_STATUS_INTERNAL_ERROR;
1132         }
1133
1134         status = schannel_check_creds_state(mem_ctx, lp_ctx,
1135                                             computer_name, received_authenticator,
1136                                             return_authenticator, creds_out);
1137         talloc_unlink(mem_ctx, lp_ctx);
1138         return status;
1139 }
1140
1141 /*************************************************************************
1142  *************************************************************************/
1143
1144 static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
1145                                                   struct auth_session_info *session_info,
1146                                                   struct messaging_context *msg_ctx,
1147                                                   const char *account_name,
1148                                                   struct samr_Password *nt_hash)
1149 {
1150         NTSTATUS status;
1151         NTSTATUS result = NT_STATUS_OK;
1152         struct dcerpc_binding_handle *h = NULL;
1153         struct tsocket_address *local;
1154         struct policy_handle user_handle;
1155         uint32_t acct_ctrl;
1156         union samr_UserInfo *info;
1157         struct samr_UserInfo18 info18;
1158         DATA_BLOB in,out;
1159         int rc;
1160         DATA_BLOB session_key;
1161
1162         ZERO_STRUCT(user_handle);
1163
1164         status = session_extract_session_key(session_info,
1165                                              &session_key,
1166                                              KEY_USE_16BYTES);
1167         if (!NT_STATUS_IS_OK(status)) {
1168                 goto out;
1169         }
1170
1171         rc = tsocket_address_inet_from_strings(mem_ctx,
1172                                                "ip",
1173                                                "127.0.0.1",
1174                                                0,
1175                                                &local);
1176         if (rc < 0) {
1177                 status = NT_STATUS_NO_MEMORY;
1178                 goto out;
1179         }
1180
1181         status = rpcint_binding_handle(mem_ctx,
1182                                        &ndr_table_samr,
1183                                        local,
1184                                        session_info,
1185                                        msg_ctx,
1186                                        &h);
1187         if (!NT_STATUS_IS_OK(status)) {
1188                 goto out;
1189         }
1190
1191         become_root();
1192         status = samr_find_machine_account(mem_ctx,
1193                                            h,
1194                                            account_name,
1195                                            SEC_FLAG_MAXIMUM_ALLOWED,
1196                                            NULL,
1197                                            NULL,
1198                                            &user_handle);
1199         unbecome_root();
1200         if (!NT_STATUS_IS_OK(status)) {
1201                 goto out;
1202         }
1203
1204         status = dcerpc_samr_QueryUserInfo2(h,
1205                                             mem_ctx,
1206                                             &user_handle,
1207                                             UserControlInformation,
1208                                             &info,
1209                                             &result);
1210         if (!NT_STATUS_IS_OK(status)) {
1211                 goto out;
1212         }
1213         if (!NT_STATUS_IS_OK(result)) {
1214                 status = result;
1215                 goto out;
1216         }
1217
1218         acct_ctrl = info->info16.acct_flags;
1219
1220         if (!(acct_ctrl & ACB_WSTRUST ||
1221               acct_ctrl & ACB_SVRTRUST ||
1222               acct_ctrl & ACB_DOMTRUST)) {
1223                 status = NT_STATUS_NO_SUCH_USER;
1224                 goto out;
1225         }
1226
1227         if (acct_ctrl & ACB_DISABLED) {
1228                 status = NT_STATUS_ACCOUNT_DISABLED;
1229                 goto out;
1230         }
1231
1232         ZERO_STRUCT(info18);
1233
1234         in = data_blob_const(nt_hash->hash, 16);
1235         out = data_blob_talloc_zero(mem_ctx, 16);
1236         sess_crypt_blob(&out, &in, &session_key, true);
1237         memcpy(info18.nt_pwd.hash, out.data, out.length);
1238
1239         info18.nt_pwd_active = true;
1240
1241         info->info18 = info18;
1242
1243         become_root();
1244         status = dcerpc_samr_SetUserInfo2(h,
1245                                           mem_ctx,
1246                                           &user_handle,
1247                                           UserInternal1Information,
1248                                           info,
1249                                           &result);
1250         unbecome_root();
1251         if (!NT_STATUS_IS_OK(status)) {
1252                 goto out;
1253         }
1254         if (!NT_STATUS_IS_OK(result)) {
1255                 status = result;
1256                 goto out;
1257         }
1258
1259  out:
1260         if (h && is_valid_policy_hnd(&user_handle)) {
1261                 dcerpc_samr_Close(h, mem_ctx, &user_handle, &result);
1262         }
1263
1264         return status;
1265 }
1266
1267 /*************************************************************************
1268  _netr_ServerPasswordSet
1269  *************************************************************************/
1270
1271 NTSTATUS _netr_ServerPasswordSet(struct pipes_struct *p,
1272                                  struct netr_ServerPasswordSet *r)
1273 {
1274         NTSTATUS status = NT_STATUS_OK;
1275         int i;
1276         struct netlogon_creds_CredentialState *creds = NULL;
1277
1278         DEBUG(5,("_netr_ServerPasswordSet: %d\n", __LINE__));
1279
1280         become_root();
1281         status = netr_creds_server_step_check(p, p->mem_ctx,
1282                                               r->in.computer_name,
1283                                               r->in.credential,
1284                                               r->out.return_authenticator,
1285                                               &creds);
1286         unbecome_root();
1287
1288         if (!NT_STATUS_IS_OK(status)) {
1289                 const char *computer_name = "<unknown>";
1290
1291                 if (creds != NULL && creds->computer_name != NULL) {
1292                         computer_name = creds->computer_name;
1293                 }
1294                 DEBUG(2,("_netr_ServerPasswordSet: netlogon_creds_server_step failed. Rejecting auth "
1295                         "request from client %s machine account %s\n",
1296                         r->in.computer_name, computer_name));
1297                 TALLOC_FREE(creds);
1298                 return status;
1299         }
1300
1301         DEBUG(3,("_netr_ServerPasswordSet: Server Password Set by remote machine:[%s] on account [%s]\n",
1302                         r->in.computer_name, creds->computer_name));
1303
1304         netlogon_creds_des_decrypt(creds, r->in.new_password);
1305
1306         DEBUG(100,("_netr_ServerPasswordSet: new given value was :\n"));
1307         for(i = 0; i < sizeof(r->in.new_password->hash); i++)
1308                 DEBUG(100,("%02X ", r->in.new_password->hash[i]));
1309         DEBUG(100,("\n"));
1310
1311         status = netr_set_machine_account_password(p->mem_ctx,
1312                                                    p->session_info,
1313                                                    p->msg_ctx,
1314                                                    creds->account_name,
1315                                                    r->in.new_password);
1316         return status;
1317 }
1318
1319 /****************************************************************
1320  _netr_ServerPasswordSet2
1321 ****************************************************************/
1322
1323 NTSTATUS _netr_ServerPasswordSet2(struct pipes_struct *p,
1324                                   struct netr_ServerPasswordSet2 *r)
1325 {
1326         NTSTATUS status;
1327         struct netlogon_creds_CredentialState *creds = NULL;
1328         DATA_BLOB plaintext;
1329         struct samr_CryptPassword password_buf;
1330         struct samr_Password nt_hash;
1331
1332         become_root();
1333         status = netr_creds_server_step_check(p, p->mem_ctx,
1334                                               r->in.computer_name,
1335                                               r->in.credential,
1336                                               r->out.return_authenticator,
1337                                               &creds);
1338         unbecome_root();
1339
1340         if (!NT_STATUS_IS_OK(status)) {
1341                 const char *computer_name = "<unknown>";
1342
1343                 if (creds && creds->computer_name) {
1344                         computer_name = creds->computer_name;
1345                 }
1346                 DEBUG(2,("_netr_ServerPasswordSet2: netlogon_creds_server_step "
1347                         "failed. Rejecting auth request from client %s machine account %s\n",
1348                         r->in.computer_name, computer_name));
1349                 TALLOC_FREE(creds);
1350                 return status;
1351         }
1352
1353         memcpy(password_buf.data, r->in.new_password->data, 512);
1354         SIVAL(password_buf.data, 512, r->in.new_password->length);
1355
1356         if (creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
1357                 netlogon_creds_aes_decrypt(creds, password_buf.data, 516);
1358         } else {
1359                 netlogon_creds_arcfour_crypt(creds, password_buf.data, 516);
1360         }
1361
1362         if (!extract_pw_from_buffer(p->mem_ctx, password_buf.data, &plaintext)) {
1363                 TALLOC_FREE(creds);
1364                 return NT_STATUS_WRONG_PASSWORD;
1365         }
1366
1367         mdfour(nt_hash.hash, plaintext.data, plaintext.length);
1368
1369         status = netr_set_machine_account_password(p->mem_ctx,
1370                                                    p->session_info,
1371                                                    p->msg_ctx,
1372                                                    creds->account_name,
1373                                                    &nt_hash);
1374         TALLOC_FREE(creds);
1375         return status;
1376 }
1377
1378 /*************************************************************************
1379  _netr_LogonSamLogoff
1380  *************************************************************************/
1381
1382 NTSTATUS _netr_LogonSamLogoff(struct pipes_struct *p,
1383                               struct netr_LogonSamLogoff *r)
1384 {
1385         NTSTATUS status;
1386         struct netlogon_creds_CredentialState *creds;
1387
1388         become_root();
1389         status = netr_creds_server_step_check(p, p->mem_ctx,
1390                                               r->in.computer_name,
1391                                               r->in.credential,
1392                                               r->out.return_authenticator,
1393                                               &creds);
1394         unbecome_root();
1395
1396         return status;
1397 }
1398
1399 static NTSTATUS _netr_LogonSamLogon_check(const struct netr_LogonSamLogonEx *r)
1400 {
1401         switch (r->in.logon_level) {
1402         case NetlogonInteractiveInformation:
1403         case NetlogonServiceInformation:
1404         case NetlogonInteractiveTransitiveInformation:
1405         case NetlogonServiceTransitiveInformation:
1406                 if (r->in.logon->password == NULL) {
1407                         return NT_STATUS_INVALID_PARAMETER;
1408                 }
1409
1410                 switch (r->in.validation_level) {
1411                 case NetlogonValidationSamInfo:  /* 2 */
1412                 case NetlogonValidationSamInfo2: /* 3 */
1413                         break;
1414                 case NetlogonValidationSamInfo4: /* 6 */
1415                         if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
1416                                 DEBUG(10,("Not adding validation info level 6 "
1417                                    "without ADS passdb backend\n"));
1418                                 return NT_STATUS_INVALID_INFO_CLASS;
1419                         }
1420                         break;
1421                 default:
1422                         return NT_STATUS_INVALID_INFO_CLASS;
1423                 }
1424
1425                 break;
1426         case NetlogonNetworkInformation:
1427         case NetlogonNetworkTransitiveInformation:
1428                 if (r->in.logon->network == NULL) {
1429                         return NT_STATUS_INVALID_PARAMETER;
1430                 }
1431
1432                 switch (r->in.validation_level) {
1433                 case NetlogonValidationSamInfo:  /* 2 */
1434                 case NetlogonValidationSamInfo2: /* 3 */
1435                         break;
1436                 case NetlogonValidationSamInfo4: /* 6 */
1437                         if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
1438                                 DEBUG(10,("Not adding validation info level 6 "
1439                                    "without ADS passdb backend\n"));
1440                                 return NT_STATUS_INVALID_INFO_CLASS;
1441                         }
1442                         break;
1443                 default:
1444                         return NT_STATUS_INVALID_INFO_CLASS;
1445                 }
1446
1447                 break;
1448
1449         case NetlogonGenericInformation:
1450                 if (r->in.logon->generic == NULL) {
1451                         return NT_STATUS_INVALID_PARAMETER;
1452                 }
1453
1454                 /* we don't support this here */
1455                 return NT_STATUS_INVALID_PARAMETER;
1456 #if 0
1457                 switch (r->in.validation_level) {
1458                 /* TODO: case NetlogonValidationGenericInfo: 4 */
1459                 case NetlogonValidationGenericInfo2: /* 5 */
1460                         break;
1461                 default:
1462                         return NT_STATUS_INVALID_INFO_CLASS;
1463                 }
1464
1465                 break;
1466 #endif
1467         default:
1468                 return NT_STATUS_INVALID_PARAMETER;
1469         }
1470
1471         return NT_STATUS_OK;
1472 }
1473
1474 /*************************************************************************
1475  _netr_LogonSamLogon_base
1476  *************************************************************************/
1477
1478 static NTSTATUS _netr_LogonSamLogon_base(struct pipes_struct *p,
1479                                          struct netr_LogonSamLogonEx *r,
1480                                          struct netlogon_creds_CredentialState *creds)
1481 {
1482         NTSTATUS status = NT_STATUS_OK;
1483         union netr_LogonLevel *logon = r->in.logon;
1484         const char *nt_username, *nt_domain, *nt_workstation;
1485         struct auth_usersupplied_info *user_info = NULL;
1486         struct auth_serversupplied_info *server_info = NULL;
1487         struct auth_context *auth_context = NULL;
1488         const char *fn;
1489
1490 #ifdef DEBUG_PASSWORD
1491         logon = netlogon_creds_shallow_copy_logon(p->mem_ctx,
1492                                                   r->in.logon_level,
1493                                                   r->in.logon);
1494         if (logon == NULL) {
1495                 logon = r->in.logon;
1496         }
1497 #endif
1498
1499         switch (p->opnum) {
1500                 case NDR_NETR_LOGONSAMLOGON:
1501                         fn = "_netr_LogonSamLogon";
1502                         break;
1503                 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
1504                         fn = "_netr_LogonSamLogonWithFlags";
1505                         break;
1506                 case NDR_NETR_LOGONSAMLOGONEX:
1507                         fn = "_netr_LogonSamLogonEx";
1508                         break;
1509                 default:
1510                         return NT_STATUS_INTERNAL_ERROR;
1511         }
1512
1513         *r->out.authoritative = true; /* authoritative response */
1514
1515         switch (r->in.validation_level) {
1516         case 2:
1517                 r->out.validation->sam2 = talloc_zero(p->mem_ctx, struct netr_SamInfo2);
1518                 if (!r->out.validation->sam2) {
1519                         return NT_STATUS_NO_MEMORY;
1520                 }
1521                 break;
1522         case 3:
1523                 r->out.validation->sam3 = talloc_zero(p->mem_ctx, struct netr_SamInfo3);
1524                 if (!r->out.validation->sam3) {
1525                         return NT_STATUS_NO_MEMORY;
1526                 }
1527                 break;
1528         case 6:
1529                 r->out.validation->sam6 = talloc_zero(p->mem_ctx, struct netr_SamInfo6);
1530                 if (!r->out.validation->sam6) {
1531                         return NT_STATUS_NO_MEMORY;
1532                 }
1533                 break;
1534         default:
1535                 DEBUG(0,("%s: bad validation_level value %d.\n",
1536                         fn, (int)r->in.validation_level));
1537                 return NT_STATUS_INVALID_INFO_CLASS;
1538         }
1539
1540         switch (r->in.logon_level) {
1541         case NetlogonInteractiveInformation:
1542         case NetlogonServiceInformation:
1543         case NetlogonInteractiveTransitiveInformation:
1544         case NetlogonServiceTransitiveInformation:
1545                 nt_username     = logon->password->identity_info.account_name.string ?
1546                                   logon->password->identity_info.account_name.string : "";
1547                 nt_domain       = logon->password->identity_info.domain_name.string ?
1548                                   logon->password->identity_info.domain_name.string : "";
1549                 nt_workstation  = logon->password->identity_info.workstation.string ?
1550                                   logon->password->identity_info.workstation.string : "";
1551
1552                 DEBUG(3,("SAM Logon (Interactive). Domain:[%s].  ", lp_workgroup()));
1553                 break;
1554         case NetlogonNetworkInformation:
1555         case NetlogonNetworkTransitiveInformation:
1556                 nt_username     = logon->network->identity_info.account_name.string ?
1557                                   logon->network->identity_info.account_name.string : "";
1558                 nt_domain       = logon->network->identity_info.domain_name.string ?
1559                                   logon->network->identity_info.domain_name.string : "";
1560                 nt_workstation  = logon->network->identity_info.workstation.string ?
1561                                   logon->network->identity_info.workstation.string : "";
1562
1563                 DEBUG(3,("SAM Logon (Network). Domain:[%s].  ", lp_workgroup()));
1564                 break;
1565         default:
1566                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1567                 return NT_STATUS_INVALID_INFO_CLASS;
1568         } /* end switch */
1569
1570         DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
1571         fstrcpy(current_user_info.smb_name, nt_username);
1572         sub_set_smb_name(nt_username);
1573
1574         DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
1575                 r->in.validation_level, nt_username));
1576
1577         status = NT_STATUS_OK;
1578
1579         netlogon_creds_decrypt_samlogon_logon(creds,
1580                                               r->in.logon_level,
1581                                               logon);
1582
1583         switch (r->in.logon_level) {
1584         case NetlogonNetworkInformation:
1585         case NetlogonNetworkTransitiveInformation:
1586         {
1587                 const char *wksname = nt_workstation;
1588
1589                 status = make_auth_context_fixed(talloc_tos(), &auth_context,
1590                                                  logon->network->challenge);
1591                 if (!NT_STATUS_IS_OK(status)) {
1592                         return status;
1593                 }
1594
1595                 /* For a network logon, the workstation name comes in with two
1596                  * backslashes in the front. Strip them if they are there. */
1597
1598                 if (*wksname == '\\') wksname++;
1599                 if (*wksname == '\\') wksname++;
1600
1601                 /* Standard challenge/response authentication */
1602                 if (!make_user_info_netlogon_network(talloc_tos(),
1603                                                      &user_info,
1604                                                      nt_username, nt_domain,
1605                                                      wksname,
1606                                                      p->remote_address,
1607                                                      logon->network->identity_info.parameter_control,
1608                                                      logon->network->lm.data,
1609                                                      logon->network->lm.length,
1610                                                      logon->network->nt.data,
1611                                                      logon->network->nt.length)) {
1612                         status = NT_STATUS_NO_MEMORY;
1613                 }
1614                 break;
1615         }
1616         case NetlogonInteractiveInformation:
1617         case NetlogonServiceInformation:
1618         case NetlogonInteractiveTransitiveInformation:
1619         case NetlogonServiceTransitiveInformation:
1620
1621                 /* 'Interactive' authentication, supplies the password in its
1622                    MD4 form, encrypted with the session key.  We will convert
1623                    this to challenge/response for the auth subsystem to chew
1624                    on */
1625         {
1626                 uint8_t chal[8];
1627
1628 #ifdef DEBUG_PASSWORD
1629                 if (logon != r->in.logon) {
1630                         DEBUG(100,("lm owf password:"));
1631                         dump_data(100,
1632                                   r->in.logon->password->lmpassword.hash, 16);
1633
1634                         DEBUG(100,("nt owf password:"));
1635                         dump_data(100,
1636                                   r->in.logon->password->ntpassword.hash, 16);
1637                 }
1638
1639                 DEBUG(100,("decrypt of lm owf password:"));
1640                 dump_data(100, logon->password->lmpassword.hash, 16);
1641
1642                 DEBUG(100,("decrypt of nt owf password:"));
1643                 dump_data(100, logon->password->ntpassword.hash, 16);
1644 #endif
1645                 status = make_auth_context_subsystem(talloc_tos(),
1646                                                      &auth_context);
1647                 if (!NT_STATUS_IS_OK(status)) {
1648                         return status;
1649                 }
1650
1651                 auth_get_ntlm_challenge(auth_context, chal);
1652
1653                 if (!make_user_info_netlogon_interactive(talloc_tos(),
1654                                                          &user_info,
1655                                                          nt_username, nt_domain,
1656                                                          nt_workstation,
1657                                                          p->remote_address,
1658                                                          logon->password->identity_info.parameter_control,
1659                                                          chal,
1660                                                          logon->password->lmpassword.hash,
1661                                                          logon->password->ntpassword.hash)) {
1662                         status = NT_STATUS_NO_MEMORY;
1663                 }
1664                 break;
1665         }
1666         default:
1667                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1668                 return NT_STATUS_INVALID_INFO_CLASS;
1669         } /* end switch */
1670
1671         if ( NT_STATUS_IS_OK(status) ) {
1672                 status = auth_check_ntlm_password(p->mem_ctx,
1673                                                   auth_context,
1674                                                   user_info,
1675                                                   &server_info);
1676         }
1677
1678         TALLOC_FREE(auth_context);
1679         TALLOC_FREE(user_info);
1680
1681         DEBUG(5,("%s: check_password returned status %s\n",
1682                   fn, nt_errstr(status)));
1683
1684         /* Check account and password */
1685
1686         if (!NT_STATUS_IS_OK(status)) {
1687                 /* If we don't know what this domain is, we need to
1688                    indicate that we are not authoritative.  This
1689                    allows the client to decide if it needs to try
1690                    a local user.  Fix by jpjanosi@us.ibm.com, #2976 */
1691                 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1692                      && !strequal(nt_domain, get_global_sam_name())
1693                      && !is_trusted_domain(nt_domain) )
1694                         *r->out.authoritative = false; /* We are not authoritative */
1695
1696                 TALLOC_FREE(server_info);
1697                 return status;
1698         }
1699
1700         if (server_info->guest) {
1701                 /* We don't like guest domain logons... */
1702                 DEBUG(5,("%s: Attempted domain logon as GUEST "
1703                          "denied.\n", fn));
1704                 TALLOC_FREE(server_info);
1705                 return NT_STATUS_LOGON_FAILURE;
1706         }
1707
1708         /* This is the point at which, if the login was successful, that
1709            the SAM Local Security Authority should record that the user is
1710            logged in to the domain.  */
1711
1712         switch (r->in.validation_level) {
1713         case 2:
1714                 status = serverinfo_to_SamInfo2(server_info,
1715                                                 r->out.validation->sam2);
1716                 break;
1717         case 3:
1718                 status = serverinfo_to_SamInfo3(server_info,
1719                                                 r->out.validation->sam3);
1720                 break;
1721         case 6:
1722                 status = serverinfo_to_SamInfo6(server_info,
1723                                                 r->out.validation->sam6);
1724                 break;
1725         }
1726
1727         TALLOC_FREE(server_info);
1728
1729         if (!NT_STATUS_IS_OK(status)) {
1730                 return status;
1731         }
1732
1733         netlogon_creds_encrypt_samlogon_validation(creds,
1734                                                    r->in.validation_level,
1735                                                    r->out.validation);
1736         return NT_STATUS_OK;
1737 }
1738
1739 /****************************************************************
1740  _netr_LogonSamLogonWithFlags
1741 ****************************************************************/
1742
1743 NTSTATUS _netr_LogonSamLogonWithFlags(struct pipes_struct *p,
1744                                       struct netr_LogonSamLogonWithFlags *r)
1745 {
1746         NTSTATUS status;
1747         struct netlogon_creds_CredentialState *creds;
1748         struct netr_LogonSamLogonEx r2;
1749         struct netr_Authenticator return_authenticator;
1750
1751         *r->out.authoritative = true;
1752
1753         r2.in.server_name       = r->in.server_name;
1754         r2.in.computer_name     = r->in.computer_name;
1755         r2.in.logon_level       = r->in.logon_level;
1756         r2.in.logon             = r->in.logon;
1757         r2.in.validation_level  = r->in.validation_level;
1758         r2.in.flags             = r->in.flags;
1759         r2.out.validation       = r->out.validation;
1760         r2.out.authoritative    = r->out.authoritative;
1761         r2.out.flags            = r->out.flags;
1762
1763         status = _netr_LogonSamLogon_check(&r2);
1764         if (!NT_STATUS_IS_OK(status)) {
1765                 return status;
1766         }
1767
1768         become_root();
1769         status = netr_creds_server_step_check(p, p->mem_ctx,
1770                                               r->in.computer_name,
1771                                               r->in.credential,
1772                                               &return_authenticator,
1773                                               &creds);
1774         unbecome_root();
1775         if (!NT_STATUS_IS_OK(status)) {
1776                 return status;
1777         }
1778
1779         status = _netr_LogonSamLogon_base(p, &r2, creds);
1780
1781         *r->out.return_authenticator = return_authenticator;
1782
1783         return status;
1784 }
1785
1786 /*************************************************************************
1787  _netr_LogonSamLogon
1788  *************************************************************************/
1789
1790 NTSTATUS _netr_LogonSamLogon(struct pipes_struct *p,
1791                              struct netr_LogonSamLogon *r)
1792 {
1793         NTSTATUS status;
1794         struct netr_LogonSamLogonWithFlags r2;
1795         uint32_t flags = 0;
1796
1797         r2.in.server_name               = r->in.server_name;
1798         r2.in.computer_name             = r->in.computer_name;
1799         r2.in.credential                = r->in.credential;
1800         r2.in.logon_level               = r->in.logon_level;
1801         r2.in.logon                     = r->in.logon;
1802         r2.in.validation_level          = r->in.validation_level;
1803         r2.in.return_authenticator      = r->in.return_authenticator;
1804         r2.in.flags                     = &flags;
1805         r2.out.validation               = r->out.validation;
1806         r2.out.authoritative            = r->out.authoritative;
1807         r2.out.flags                    = &flags;
1808         r2.out.return_authenticator     = r->out.return_authenticator;
1809
1810         status = _netr_LogonSamLogonWithFlags(p, &r2);
1811
1812         return status;
1813 }
1814
1815 /*************************************************************************
1816  _netr_LogonSamLogonEx
1817  - no credential chaining. Map into net sam logon.
1818  *************************************************************************/
1819
1820 NTSTATUS _netr_LogonSamLogonEx(struct pipes_struct *p,
1821                                struct netr_LogonSamLogonEx *r)
1822 {
1823         NTSTATUS status;
1824         struct netlogon_creds_CredentialState *creds = NULL;
1825         struct loadparm_context *lp_ctx;
1826
1827         *r->out.authoritative = true;
1828
1829         status = _netr_LogonSamLogon_check(r);
1830         if (!NT_STATUS_IS_OK(status)) {
1831                 return status;
1832         }
1833
1834         /* Only allow this if the pipe is protected. */
1835         if (p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
1836                 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1837                         get_remote_machine_name() ));
1838                 return NT_STATUS_INVALID_PARAMETER;
1839         }
1840
1841         lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_helpers());
1842         if (lp_ctx == NULL) {
1843                 DEBUG(0, ("loadparm_init_s3 failed\n"));
1844                 return NT_STATUS_INTERNAL_ERROR;
1845         }
1846
1847         become_root();
1848         status = schannel_get_creds_state(p->mem_ctx, lp_ctx,
1849                                           r->in.computer_name, &creds);
1850         unbecome_root();
1851         talloc_unlink(p->mem_ctx, lp_ctx);
1852
1853         if (!NT_STATUS_IS_OK(status)) {
1854                 return status;
1855         }
1856
1857         status = _netr_LogonSamLogon_base(p, r, creds);
1858         TALLOC_FREE(creds);
1859
1860         return status;
1861 }
1862
1863 /*************************************************************************
1864  _ds_enum_dom_trusts
1865  *************************************************************************/
1866 #if 0   /* JERRY -- not correct */
1867  NTSTATUS _ds_enum_dom_trusts(struct pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1868                              DS_R_ENUM_DOM_TRUSTS *r_u)
1869 {
1870         NTSTATUS status = NT_STATUS_OK;
1871
1872         /* TODO: According to MSDN, the can only be executed against a
1873            DC or domain member running Windows 2000 or later.  Need
1874            to test against a standalone 2k server and see what it
1875            does.  A windows 2000 DC includes its own domain in the
1876            list.  --jerry */
1877
1878         return status;
1879 }
1880 #endif  /* JERRY */
1881
1882
1883 /****************************************************************
1884 ****************************************************************/
1885
1886 WERROR _netr_LogonUasLogon(struct pipes_struct *p,
1887                            struct netr_LogonUasLogon *r)
1888 {
1889         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1890         return WERR_NOT_SUPPORTED;
1891 }
1892
1893 /****************************************************************
1894 ****************************************************************/
1895
1896 WERROR _netr_LogonUasLogoff(struct pipes_struct *p,
1897                             struct netr_LogonUasLogoff *r)
1898 {
1899         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1900         return WERR_NOT_SUPPORTED;
1901 }
1902
1903 /****************************************************************
1904 ****************************************************************/
1905
1906 NTSTATUS _netr_DatabaseDeltas(struct pipes_struct *p,
1907                               struct netr_DatabaseDeltas *r)
1908 {
1909         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1910         return NT_STATUS_NOT_IMPLEMENTED;
1911 }
1912
1913 /****************************************************************
1914 ****************************************************************/
1915
1916 NTSTATUS _netr_DatabaseSync(struct pipes_struct *p,
1917                             struct netr_DatabaseSync *r)
1918 {
1919         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1920         return NT_STATUS_NOT_IMPLEMENTED;
1921 }
1922
1923 /****************************************************************
1924 ****************************************************************/
1925
1926 NTSTATUS _netr_AccountDeltas(struct pipes_struct *p,
1927                              struct netr_AccountDeltas *r)
1928 {
1929         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1930         return NT_STATUS_NOT_IMPLEMENTED;
1931 }
1932
1933 /****************************************************************
1934 ****************************************************************/
1935
1936 NTSTATUS _netr_AccountSync(struct pipes_struct *p,
1937                            struct netr_AccountSync *r)
1938 {
1939         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1940         return NT_STATUS_NOT_IMPLEMENTED;
1941 }
1942
1943 /****************************************************************
1944 ****************************************************************/
1945
1946 static bool wb_getdcname(TALLOC_CTX *mem_ctx,
1947                          const char *domain,
1948                          const char **dcname,
1949                          uint32_t flags,
1950                          WERROR *werr)
1951 {
1952         wbcErr result;
1953         struct wbcDomainControllerInfo *dc_info = NULL;
1954
1955         result = wbcLookupDomainController(domain,
1956                                            flags,
1957                                            &dc_info);
1958         switch (result) {
1959         case WBC_ERR_SUCCESS:
1960                 break;
1961         case WBC_ERR_WINBIND_NOT_AVAILABLE:
1962                 return false;
1963         case WBC_ERR_DOMAIN_NOT_FOUND:
1964                 *werr = WERR_NO_SUCH_DOMAIN;
1965                 return true;
1966         default:
1967                 *werr = WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1968                 return true;
1969         }
1970
1971         *dcname = talloc_strdup(mem_ctx, dc_info->dc_name);
1972         wbcFreeMemory(dc_info);
1973         if (!*dcname) {
1974                 *werr = WERR_NOMEM;
1975                 return false;
1976         }
1977
1978         *werr = WERR_OK;
1979
1980         return true;
1981 }
1982
1983 /****************************************************************
1984  _netr_GetDcName
1985 ****************************************************************/
1986
1987 WERROR _netr_GetDcName(struct pipes_struct *p,
1988                        struct netr_GetDcName *r)
1989 {
1990         NTSTATUS status;
1991         WERROR werr;
1992         uint32_t flags;
1993         struct netr_DsRGetDCNameInfo *info;
1994         bool ret;
1995
1996         ret = wb_getdcname(p->mem_ctx,
1997                            r->in.domainname,
1998                            r->out.dcname,
1999                            WBC_LOOKUP_DC_IS_FLAT_NAME |
2000                            WBC_LOOKUP_DC_RETURN_FLAT_NAME |
2001                            WBC_LOOKUP_DC_PDC_REQUIRED,
2002                            &werr);
2003         if (ret == true) {
2004                 return werr;
2005         }
2006
2007         flags = DS_PDC_REQUIRED | DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
2008
2009         status = dsgetdcname(p->mem_ctx,
2010                              p->msg_ctx,
2011                              r->in.domainname,
2012                              NULL,
2013                              NULL,
2014                              flags,
2015                              &info);
2016         if (!NT_STATUS_IS_OK(status)) {
2017                 return ntstatus_to_werror(status);
2018         }
2019
2020         *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
2021         talloc_free(info);
2022         if (!*r->out.dcname) {
2023                 return WERR_NOMEM;
2024         }
2025
2026         return WERR_OK;
2027 }
2028
2029 /****************************************************************
2030  _netr_GetAnyDCName
2031 ****************************************************************/
2032
2033 WERROR _netr_GetAnyDCName(struct pipes_struct *p,
2034                           struct netr_GetAnyDCName *r)
2035 {
2036         NTSTATUS status;
2037         WERROR werr;
2038         uint32_t flags;
2039         struct netr_DsRGetDCNameInfo *info;
2040         bool ret;
2041
2042         ret = wb_getdcname(p->mem_ctx,
2043                            r->in.domainname,
2044                            r->out.dcname,
2045                            WBC_LOOKUP_DC_IS_FLAT_NAME |
2046                            WBC_LOOKUP_DC_RETURN_FLAT_NAME,
2047                            &werr);
2048         if (ret == true) {
2049                 return werr;
2050         }
2051
2052         flags = DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
2053
2054         status = dsgetdcname(p->mem_ctx,
2055                              p->msg_ctx,
2056                              r->in.domainname,
2057                              NULL,
2058                              NULL,
2059                              flags,
2060                              &info);
2061         if (!NT_STATUS_IS_OK(status)) {
2062                 return ntstatus_to_werror(status);
2063         }
2064
2065         *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
2066         talloc_free(info);
2067         if (!*r->out.dcname) {
2068                 return WERR_NOMEM;
2069         }
2070
2071         return WERR_OK;
2072 }
2073
2074 /****************************************************************
2075 ****************************************************************/
2076
2077 NTSTATUS _netr_DatabaseSync2(struct pipes_struct *p,
2078                              struct netr_DatabaseSync2 *r)
2079 {
2080         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2081         return NT_STATUS_NOT_IMPLEMENTED;
2082 }
2083
2084 /****************************************************************
2085 ****************************************************************/
2086
2087 NTSTATUS _netr_DatabaseRedo(struct pipes_struct *p,
2088                             struct netr_DatabaseRedo *r)
2089 {
2090         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2091         return NT_STATUS_NOT_IMPLEMENTED;
2092 }
2093
2094 /****************************************************************
2095 ****************************************************************/
2096
2097 WERROR _netr_DsRGetDCName(struct pipes_struct *p,
2098                           struct netr_DsRGetDCName *r)
2099 {
2100         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2101         return WERR_NOT_SUPPORTED;
2102 }
2103
2104 /****************************************************************
2105 ****************************************************************/
2106
2107 NTSTATUS _netr_LogonGetCapabilities(struct pipes_struct *p,
2108                                     struct netr_LogonGetCapabilities *r)
2109 {
2110         struct netlogon_creds_CredentialState *creds;
2111         NTSTATUS status;
2112
2113         become_root();
2114         status = netr_creds_server_step_check(p, p->mem_ctx,
2115                                               r->in.computer_name,
2116                                               r->in.credential,
2117                                               r->out.return_authenticator,
2118                                               &creds);
2119         unbecome_root();
2120         if (!NT_STATUS_IS_OK(status)) {
2121                 return status;
2122         }
2123
2124         if (r->in.query_level != 1) {
2125                 return NT_STATUS_NOT_SUPPORTED;
2126         }
2127
2128         r->out.capabilities->server_capabilities = creds->negotiate_flags;
2129
2130         return NT_STATUS_OK;
2131 }
2132
2133 /****************************************************************
2134 ****************************************************************/
2135
2136 WERROR _netr_NETRLOGONSETSERVICEBITS(struct pipes_struct *p,
2137                                      struct netr_NETRLOGONSETSERVICEBITS *r)
2138 {
2139         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2140         return WERR_NOT_SUPPORTED;
2141 }
2142
2143 /****************************************************************
2144 ****************************************************************/
2145
2146 WERROR _netr_LogonGetTrustRid(struct pipes_struct *p,
2147                               struct netr_LogonGetTrustRid *r)
2148 {
2149         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2150         return WERR_NOT_SUPPORTED;
2151 }
2152
2153 /****************************************************************
2154 ****************************************************************/
2155
2156 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(struct pipes_struct *p,
2157                                           struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
2158 {
2159         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2160         return WERR_NOT_SUPPORTED;
2161 }
2162
2163 /****************************************************************
2164 ****************************************************************/
2165
2166 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(struct pipes_struct *p,
2167                                           struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
2168 {
2169         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2170         return WERR_NOT_SUPPORTED;
2171 }
2172
2173 /****************************************************************
2174 ****************************************************************/
2175
2176 WERROR _netr_DsRGetDCNameEx(struct pipes_struct *p,
2177                             struct netr_DsRGetDCNameEx *r)
2178 {
2179         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2180         return WERR_NOT_SUPPORTED;
2181 }
2182
2183 /****************************************************************
2184 ****************************************************************/
2185
2186 WERROR _netr_DsRGetSiteName(struct pipes_struct *p,
2187                             struct netr_DsRGetSiteName *r)
2188 {
2189         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2190         return WERR_NOT_SUPPORTED;
2191 }
2192
2193 /****************************************************************
2194 ****************************************************************/
2195
2196 NTSTATUS _netr_LogonGetDomainInfo(struct pipes_struct *p,
2197                                   struct netr_LogonGetDomainInfo *r)
2198 {
2199         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2200         return NT_STATUS_NOT_IMPLEMENTED;
2201 }
2202
2203 /****************************************************************
2204 ****************************************************************/
2205
2206 NTSTATUS _netr_ServerPasswordGet(struct pipes_struct *p,
2207                                  struct netr_ServerPasswordGet *r)
2208 {
2209         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2210         return NT_STATUS_NOT_SUPPORTED;
2211 }
2212
2213 /****************************************************************
2214 ****************************************************************/
2215
2216 WERROR _netr_NETRLOGONSENDTOSAM(struct pipes_struct *p,
2217                                 struct netr_NETRLOGONSENDTOSAM *r)
2218 {
2219         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2220         return WERR_NOT_SUPPORTED;
2221 }
2222
2223 /****************************************************************
2224 ****************************************************************/
2225
2226 WERROR _netr_DsRAddressToSitenamesW(struct pipes_struct *p,
2227                                     struct netr_DsRAddressToSitenamesW *r)
2228 {
2229         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2230         return WERR_NOT_SUPPORTED;
2231 }
2232
2233 /****************************************************************
2234 ****************************************************************/
2235
2236 WERROR _netr_DsRGetDCNameEx2(struct pipes_struct *p,
2237                              struct netr_DsRGetDCNameEx2 *r)
2238 {
2239         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2240         return WERR_NOT_SUPPORTED;
2241 }
2242
2243 /****************************************************************
2244 ****************************************************************/
2245
2246 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(struct pipes_struct *p,
2247                                                  struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
2248 {
2249         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2250         return WERR_NOT_SUPPORTED;
2251 }
2252
2253 /****************************************************************
2254 ****************************************************************/
2255
2256 WERROR _netr_NetrEnumerateTrustedDomainsEx(struct pipes_struct *p,
2257                                            struct netr_NetrEnumerateTrustedDomainsEx *r)
2258 {
2259         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2260         return WERR_NOT_SUPPORTED;
2261 }
2262
2263 /****************************************************************
2264 ****************************************************************/
2265
2266 WERROR _netr_DsRAddressToSitenamesExW(struct pipes_struct *p,
2267                                       struct netr_DsRAddressToSitenamesExW *r)
2268 {
2269         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2270         return WERR_NOT_SUPPORTED;
2271 }
2272
2273 /****************************************************************
2274 ****************************************************************/
2275
2276 WERROR _netr_DsrGetDcSiteCoverageW(struct pipes_struct *p,
2277                                    struct netr_DsrGetDcSiteCoverageW *r)
2278 {
2279         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2280         return WERR_NOT_SUPPORTED;
2281 }
2282
2283 /****************************************************************
2284 ****************************************************************/
2285
2286 WERROR _netr_DsrEnumerateDomainTrusts(struct pipes_struct *p,
2287                                       struct netr_DsrEnumerateDomainTrusts *r)
2288 {
2289         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2290         return WERR_NOT_SUPPORTED;
2291 }
2292
2293 /****************************************************************
2294 ****************************************************************/
2295
2296 WERROR _netr_DsrDeregisterDNSHostRecords(struct pipes_struct *p,
2297                                          struct netr_DsrDeregisterDNSHostRecords *r)
2298 {
2299         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2300         return WERR_NOT_SUPPORTED;
2301 }
2302
2303 /****************************************************************
2304 ****************************************************************/
2305
2306 NTSTATUS _netr_ServerTrustPasswordsGet(struct pipes_struct *p,
2307                                        struct netr_ServerTrustPasswordsGet *r)
2308 {
2309         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2310         return NT_STATUS_NOT_IMPLEMENTED;
2311 }
2312
2313 /****************************************************************
2314 ****************************************************************/
2315
2316 static NTSTATUS fill_forest_trust_array(TALLOC_CTX *mem_ctx,
2317                                         struct lsa_ForestTrustInformation *info)
2318 {
2319         struct lsa_ForestTrustRecord *e;
2320         struct pdb_domain_info *dom_info;
2321         struct lsa_ForestTrustDomainInfo *domain_info;
2322         char **upn_suffixes = NULL;
2323         uint32_t num_suffixes = 0;
2324         uint32_t i = 0;
2325         NTSTATUS status;
2326
2327         dom_info = pdb_get_domain_info(mem_ctx);
2328         if (dom_info == NULL) {
2329                 return NT_STATUS_NO_MEMORY;
2330         }
2331
2332         info->count = 2;
2333
2334         become_root();
2335         status = pdb_enum_upn_suffixes(info, &num_suffixes, &upn_suffixes);
2336         unbecome_root();
2337         if (NT_STATUS_IS_OK(status) && (num_suffixes > 0)) {
2338                 info->count += num_suffixes;
2339         }
2340
2341         info->entries = talloc_array(info, struct lsa_ForestTrustRecord *, info->count);
2342         if (info->entries == NULL) {
2343                 return NT_STATUS_NO_MEMORY;
2344         }
2345
2346         e = talloc(info, struct lsa_ForestTrustRecord);
2347         if (e == NULL) {
2348                 return NT_STATUS_NO_MEMORY;
2349         }
2350
2351         e->flags = 0;
2352         e->type = LSA_FOREST_TRUST_TOP_LEVEL_NAME;
2353         e->time = 0; /* so far always 0 in trces. */
2354         e->forest_trust_data.top_level_name.string = talloc_steal(info,
2355                                                                   dom_info->dns_forest);
2356
2357         info->entries[0] = e;
2358
2359         if (num_suffixes > 0) {
2360                 for (i = 0; i < num_suffixes ; i++) {
2361                         e = talloc(info, struct lsa_ForestTrustRecord);
2362                         if (e == NULL) {
2363                                 return NT_STATUS_NO_MEMORY;
2364                         }
2365
2366                         e->flags = 0;
2367                         e->type = LSA_FOREST_TRUST_TOP_LEVEL_NAME;
2368                         e->time = 0; /* so far always 0 in traces. */
2369                         e->forest_trust_data.top_level_name.string = upn_suffixes[i];
2370                         info->entries[1 + i] = e;
2371                 }
2372         }
2373
2374         e = talloc(info, struct lsa_ForestTrustRecord);
2375         if (e == NULL) {
2376                 return NT_STATUS_NO_MEMORY;
2377         }
2378
2379         /* TODO: check if disabled and set flags accordingly */
2380         e->flags = 0;
2381         e->type = LSA_FOREST_TRUST_DOMAIN_INFO;
2382         e->time = 0; /* so far always 0 in traces. */
2383
2384         domain_info = &e->forest_trust_data.domain_info;
2385         domain_info->domain_sid = dom_sid_dup(info, &dom_info->sid);
2386
2387         domain_info->dns_domain_name.string = talloc_steal(info,
2388                                                            dom_info->dns_domain);
2389         domain_info->netbios_domain_name.string = talloc_steal(info,
2390                                                                dom_info->name);
2391
2392         info->entries[info->count - 1] = e;
2393
2394         return NT_STATUS_OK;
2395 }
2396
2397 /****************************************************************
2398 ****************************************************************/
2399
2400 WERROR _netr_DsRGetForestTrustInformation(struct pipes_struct *p,
2401                                           struct netr_DsRGetForestTrustInformation *r)
2402 {
2403         NTSTATUS status;
2404         struct lsa_ForestTrustInformation *info, **info_ptr;
2405
2406         if (!(p->pipe_bound && (p->auth.auth_type != DCERPC_AUTH_TYPE_NONE)
2407                        && (p->auth.auth_level != DCERPC_AUTH_LEVEL_NONE))) {
2408                 p->fault_state = DCERPC_FAULT_ACCESS_DENIED;
2409                 return WERR_ACCESS_DENIED;
2410         }
2411
2412         if (r->in.flags & (~DS_GFTI_UPDATE_TDO)) {
2413                 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2414                 return WERR_INVALID_FLAGS;
2415         }
2416
2417         if ((r->in.flags & DS_GFTI_UPDATE_TDO) && (lp_server_role() != ROLE_DOMAIN_PDC)) {
2418                 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2419                 return WERR_NERR_NOTPRIMARY;
2420         }
2421
2422         if ((r->in.trusted_domain_name == NULL) && (r->in.flags & DS_GFTI_UPDATE_TDO)) {
2423                 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2424                 return WERR_INVALID_PARAMETER;
2425         }
2426
2427         /* retrieve forest trust information and stop further processing */
2428         if (r->in.trusted_domain_name == NULL) {
2429                 info_ptr = talloc(p->mem_ctx, struct lsa_ForestTrustInformation *);
2430                 if (info_ptr == NULL) {
2431                         p->fault_state = DCERPC_FAULT_CANT_PERFORM;
2432                         return WERR_NOMEM;
2433                 }
2434                 info = talloc_zero(info_ptr, struct lsa_ForestTrustInformation);
2435                 if (info == NULL) {
2436                         p->fault_state = DCERPC_FAULT_CANT_PERFORM;
2437                         return WERR_NOMEM;
2438                 }
2439
2440                 /* Fill forest trust information and expand UPN suffixes list */
2441                 status = fill_forest_trust_array(p->mem_ctx, info);
2442                 if (!NT_STATUS_IS_OK(status)) {
2443                         p->fault_state = DCERPC_FAULT_CANT_PERFORM;
2444                         return WERR_NOMEM;
2445                 }
2446
2447                 *info_ptr = info;
2448                 r->out.forest_trust_info = info_ptr;
2449
2450                 return WERR_OK;
2451
2452         }
2453
2454         /* TODO: implement remaining parts of DsrGetForestTrustInformation (opnum 43)
2455          *       when trusted_domain_name is not NULL */
2456
2457         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2458         return WERR_NOT_SUPPORTED;
2459 }
2460
2461 /****************************************************************
2462  _netr_GetForestTrustInformation
2463 ****************************************************************/
2464
2465 NTSTATUS _netr_GetForestTrustInformation(struct pipes_struct *p,
2466                                          struct netr_GetForestTrustInformation *r)
2467 {
2468         NTSTATUS status;
2469         struct netlogon_creds_CredentialState *creds;
2470         struct lsa_ForestTrustInformation *info, **info_ptr;
2471
2472         /* TODO: check server name */
2473
2474         become_root();
2475         status = netr_creds_server_step_check(p, p->mem_ctx,
2476                                               r->in.computer_name,
2477                                               r->in.credential,
2478                                               r->out.return_authenticator,
2479                                               &creds);
2480         unbecome_root();
2481         if (!NT_STATUS_IS_OK(status)) {
2482                 return status;
2483         }
2484
2485         if ((creds->secure_channel_type != SEC_CHAN_DNS_DOMAIN) &&
2486             (creds->secure_channel_type != SEC_CHAN_DOMAIN)) {
2487                 return NT_STATUS_NOT_IMPLEMENTED;
2488         }
2489
2490         info_ptr = talloc(p->mem_ctx, struct lsa_ForestTrustInformation *);
2491         if (!info_ptr) {
2492                 return NT_STATUS_NO_MEMORY;
2493         }
2494         info = talloc_zero(info_ptr, struct lsa_ForestTrustInformation);
2495         if (!info) {
2496                 return NT_STATUS_NO_MEMORY;
2497         }
2498
2499         /* Fill forest trust information, do expand UPN suffixes list */
2500         status = fill_forest_trust_array(p->mem_ctx, info);
2501         if (!NT_STATUS_IS_OK(status)) {
2502                 return status;
2503         }
2504
2505         *info_ptr = info;
2506         r->out.forest_trust_info = info_ptr;
2507
2508         return NT_STATUS_OK;
2509 }
2510
2511 /****************************************************************
2512 ****************************************************************/
2513
2514 static NTSTATUS get_password_from_trustAuth(TALLOC_CTX *mem_ctx,
2515                                             const DATA_BLOB *trustAuth_blob,
2516                                             struct netlogon_creds_CredentialState *creds,
2517                                             struct samr_Password *current_pw_enc,
2518                                             struct samr_Password *previous_pw_enc)
2519 {
2520         enum ndr_err_code ndr_err;
2521         struct trustAuthInOutBlob trustAuth;
2522
2523         ndr_err = ndr_pull_struct_blob_all(trustAuth_blob, mem_ctx, &trustAuth,
2524                                            (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2525         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2526                 return NT_STATUS_UNSUCCESSFUL;
2527         }
2528
2529         if (trustAuth.count != 0 && trustAuth.current.count != 0 &&
2530             trustAuth.current.array[0].AuthType == TRUST_AUTH_TYPE_CLEAR) {
2531                 mdfour(current_pw_enc->hash,
2532                        trustAuth.current.array[0].AuthInfo.clear.password,
2533                        trustAuth.current.array[0].AuthInfo.clear.size);
2534                 netlogon_creds_des_encrypt(creds, current_pw_enc);
2535         } else {
2536                 return NT_STATUS_UNSUCCESSFUL;
2537         }
2538
2539
2540         if (trustAuth.previous.count != 0 &&
2541             trustAuth.previous.array[0].AuthType == TRUST_AUTH_TYPE_CLEAR) {
2542                 mdfour(previous_pw_enc->hash,
2543                        trustAuth.previous.array[0].AuthInfo.clear.password,
2544                        trustAuth.previous.array[0].AuthInfo.clear.size);
2545                 netlogon_creds_des_encrypt(creds, previous_pw_enc);
2546         } else {
2547                 ZERO_STRUCTP(previous_pw_enc);
2548         }
2549
2550         return NT_STATUS_OK;
2551 }
2552
2553 /****************************************************************
2554  _netr_ServerGetTrustInfo
2555 ****************************************************************/
2556
2557 NTSTATUS _netr_ServerGetTrustInfo(struct pipes_struct *p,
2558                                   struct netr_ServerGetTrustInfo *r)
2559 {
2560         NTSTATUS status;
2561         struct netlogon_creds_CredentialState *creds;
2562         char *account_name;
2563         size_t account_name_last;
2564         bool trusted;
2565         struct netr_TrustInfo *trust_info;
2566         struct pdb_trusted_domain *td;
2567
2568         /* TODO: check server name */
2569
2570         become_root();
2571         status = netr_creds_server_step_check(p, p->mem_ctx,
2572                                               r->in.computer_name,
2573                                               r->in.credential,
2574                                               r->out.return_authenticator,
2575                                               &creds);
2576         unbecome_root();
2577         if (!NT_STATUS_IS_OK(status)) {
2578                 return status;
2579         }
2580
2581         account_name = talloc_strdup(p->mem_ctx, r->in.account_name);
2582         if (account_name == NULL) {
2583                 return NT_STATUS_NO_MEMORY;
2584         }
2585
2586         account_name_last = strlen(account_name);
2587         if (account_name_last == 0) {
2588                 return NT_STATUS_INVALID_PARAMETER;
2589         }
2590         account_name_last--;
2591         if (account_name[account_name_last] == '.') {
2592                 account_name[account_name_last] = '\0';
2593         }
2594
2595         if ((creds->secure_channel_type != SEC_CHAN_DNS_DOMAIN) &&
2596             (creds->secure_channel_type != SEC_CHAN_DOMAIN)) {
2597                 trusted = false;
2598         } else {
2599                 trusted = true;
2600         }
2601
2602
2603         if (trusted) {
2604                 account_name_last = strlen(account_name);
2605                 if (account_name_last == 0) {
2606                         return NT_STATUS_INVALID_PARAMETER;
2607                 }
2608                 account_name_last--;
2609                 if (account_name[account_name_last] == '$') {
2610                         account_name[account_name_last] = '\0';
2611                 }
2612
2613                 status = pdb_get_trusted_domain(p->mem_ctx, account_name, &td);
2614                 if (!NT_STATUS_IS_OK(status)) {
2615                         return status;
2616                 }
2617
2618                 if (r->out.trust_info != NULL) {
2619                         trust_info = talloc_zero(p->mem_ctx, struct netr_TrustInfo);
2620                         if (trust_info == NULL) {
2621                                 return NT_STATUS_NO_MEMORY;
2622                         }
2623                         trust_info->count = 1;
2624
2625                         trust_info->data = talloc_array(trust_info, uint32_t, 1);
2626                         if (trust_info->data == NULL) {
2627                                 return NT_STATUS_NO_MEMORY;
2628                         }
2629                         trust_info->data[0] = td->trust_attributes;
2630
2631                         *r->out.trust_info = trust_info;
2632                 }
2633
2634                 if (td->trust_auth_incoming.data == NULL) {
2635                         return NT_STATUS_INVALID_PARAMETER;
2636                 }
2637
2638                 status = get_password_from_trustAuth(p->mem_ctx,
2639                                                      &td->trust_auth_incoming,
2640                                                      creds,
2641                                                      r->out.new_owf_password,
2642                                                      r->out.old_owf_password);
2643
2644                 if (!NT_STATUS_IS_OK(status)) {
2645                         return status;
2646                 }
2647
2648         } else {
2649 /* TODO: look for machine password */
2650                 ZERO_STRUCTP(r->out.new_owf_password);
2651                 ZERO_STRUCTP(r->out.old_owf_password);
2652
2653                 return NT_STATUS_NOT_IMPLEMENTED;
2654         }
2655
2656         return NT_STATUS_OK;
2657 }
2658
2659 /****************************************************************
2660 ****************************************************************/
2661
2662 NTSTATUS _netr_Unused47(struct pipes_struct *p,
2663                         struct netr_Unused47 *r)
2664 {
2665         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2666         return NT_STATUS_NOT_IMPLEMENTED;
2667 }
2668
2669 /****************************************************************
2670 ****************************************************************/
2671
2672 NTSTATUS _netr_DsrUpdateReadOnlyServerDnsRecords(struct pipes_struct *p,
2673                                                  struct netr_DsrUpdateReadOnlyServerDnsRecords *r)
2674 {
2675         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2676         return NT_STATUS_NOT_IMPLEMENTED;
2677 }