CVE-2016-2111: s3:rpc_server/netlogon: check NTLMv2_RESPONSE values for SEC_CHAN_WKSTA
[samba.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                 const char *workgroup = lp_workgroup();
1589
1590                 status = make_auth_context_fixed(talloc_tos(), &auth_context,
1591                                                  logon->network->challenge);
1592                 if (!NT_STATUS_IS_OK(status)) {
1593                         return status;
1594                 }
1595
1596                 /* For a network logon, the workstation name comes in with two
1597                  * backslashes in the front. Strip them if they are there. */
1598
1599                 if (*wksname == '\\') wksname++;
1600                 if (*wksname == '\\') wksname++;
1601
1602                 /* Standard challenge/response authentication */
1603                 if (!make_user_info_netlogon_network(talloc_tos(),
1604                                                      &user_info,
1605                                                      nt_username, nt_domain,
1606                                                      wksname,
1607                                                      p->remote_address,
1608                                                      logon->network->identity_info.parameter_control,
1609                                                      logon->network->lm.data,
1610                                                      logon->network->lm.length,
1611                                                      logon->network->nt.data,
1612                                                      logon->network->nt.length)) {
1613                         status = NT_STATUS_NO_MEMORY;
1614                 }
1615
1616                 if (NT_STATUS_IS_OK(status)) {
1617                         status = NTLMv2_RESPONSE_verify_netlogon_creds(
1618                                                 user_info->client.account_name,
1619                                                 user_info->client.domain_name,
1620                                                 user_info->password.response.nt,
1621                                                 creds, workgroup);
1622                 }
1623                 break;
1624         }
1625         case NetlogonInteractiveInformation:
1626         case NetlogonServiceInformation:
1627         case NetlogonInteractiveTransitiveInformation:
1628         case NetlogonServiceTransitiveInformation:
1629
1630                 /* 'Interactive' authentication, supplies the password in its
1631                    MD4 form, encrypted with the session key.  We will convert
1632                    this to challenge/response for the auth subsystem to chew
1633                    on */
1634         {
1635                 uint8_t chal[8];
1636
1637 #ifdef DEBUG_PASSWORD
1638                 if (logon != r->in.logon) {
1639                         DEBUG(100,("lm owf password:"));
1640                         dump_data(100,
1641                                   r->in.logon->password->lmpassword.hash, 16);
1642
1643                         DEBUG(100,("nt owf password:"));
1644                         dump_data(100,
1645                                   r->in.logon->password->ntpassword.hash, 16);
1646                 }
1647
1648                 DEBUG(100,("decrypt of lm owf password:"));
1649                 dump_data(100, logon->password->lmpassword.hash, 16);
1650
1651                 DEBUG(100,("decrypt of nt owf password:"));
1652                 dump_data(100, logon->password->ntpassword.hash, 16);
1653 #endif
1654                 status = make_auth_context_subsystem(talloc_tos(),
1655                                                      &auth_context);
1656                 if (!NT_STATUS_IS_OK(status)) {
1657                         return status;
1658                 }
1659
1660                 auth_get_ntlm_challenge(auth_context, chal);
1661
1662                 if (!make_user_info_netlogon_interactive(talloc_tos(),
1663                                                          &user_info,
1664                                                          nt_username, nt_domain,
1665                                                          nt_workstation,
1666                                                          p->remote_address,
1667                                                          logon->password->identity_info.parameter_control,
1668                                                          chal,
1669                                                          logon->password->lmpassword.hash,
1670                                                          logon->password->ntpassword.hash)) {
1671                         status = NT_STATUS_NO_MEMORY;
1672                 }
1673                 break;
1674         }
1675         default:
1676                 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1677                 return NT_STATUS_INVALID_INFO_CLASS;
1678         } /* end switch */
1679
1680         if ( NT_STATUS_IS_OK(status) ) {
1681                 status = auth_check_ntlm_password(p->mem_ctx,
1682                                                   auth_context,
1683                                                   user_info,
1684                                                   &server_info);
1685         }
1686
1687         TALLOC_FREE(auth_context);
1688         TALLOC_FREE(user_info);
1689
1690         DEBUG(5,("%s: check_password returned status %s\n",
1691                   fn, nt_errstr(status)));
1692
1693         /* Check account and password */
1694
1695         if (!NT_STATUS_IS_OK(status)) {
1696                 /* If we don't know what this domain is, we need to
1697                    indicate that we are not authoritative.  This
1698                    allows the client to decide if it needs to try
1699                    a local user.  Fix by jpjanosi@us.ibm.com, #2976 */
1700                 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1701                      && !strequal(nt_domain, get_global_sam_name())
1702                      && !is_trusted_domain(nt_domain) )
1703                         *r->out.authoritative = false; /* We are not authoritative */
1704
1705                 TALLOC_FREE(server_info);
1706                 return status;
1707         }
1708
1709         if (server_info->guest) {
1710                 /* We don't like guest domain logons... */
1711                 DEBUG(5,("%s: Attempted domain logon as GUEST "
1712                          "denied.\n", fn));
1713                 TALLOC_FREE(server_info);
1714                 return NT_STATUS_LOGON_FAILURE;
1715         }
1716
1717         /* This is the point at which, if the login was successful, that
1718            the SAM Local Security Authority should record that the user is
1719            logged in to the domain.  */
1720
1721         switch (r->in.validation_level) {
1722         case 2:
1723                 status = serverinfo_to_SamInfo2(server_info,
1724                                                 r->out.validation->sam2);
1725                 break;
1726         case 3:
1727                 status = serverinfo_to_SamInfo3(server_info,
1728                                                 r->out.validation->sam3);
1729                 break;
1730         case 6:
1731                 /* Only allow this if the pipe is protected. */
1732                 if (p->auth.auth_level < DCERPC_AUTH_LEVEL_PRIVACY) {
1733                         DEBUG(0,("netr_Validation6: client %s not using privacy for netlogon\n",
1734                                 get_remote_machine_name()));
1735                         status = NT_STATUS_INVALID_PARAMETER;
1736                         break;
1737                 }
1738
1739                 status = serverinfo_to_SamInfo6(server_info,
1740                                                 r->out.validation->sam6);
1741                 break;
1742         }
1743
1744         TALLOC_FREE(server_info);
1745
1746         if (!NT_STATUS_IS_OK(status)) {
1747                 return status;
1748         }
1749
1750         netlogon_creds_encrypt_samlogon_validation(creds,
1751                                                    r->in.validation_level,
1752                                                    r->out.validation);
1753         return NT_STATUS_OK;
1754 }
1755
1756 /****************************************************************
1757  _netr_LogonSamLogonWithFlags
1758 ****************************************************************/
1759
1760 NTSTATUS _netr_LogonSamLogonWithFlags(struct pipes_struct *p,
1761                                       struct netr_LogonSamLogonWithFlags *r)
1762 {
1763         NTSTATUS status;
1764         struct netlogon_creds_CredentialState *creds;
1765         struct netr_LogonSamLogonEx r2;
1766         struct netr_Authenticator return_authenticator;
1767
1768         *r->out.authoritative = true;
1769
1770         r2.in.server_name       = r->in.server_name;
1771         r2.in.computer_name     = r->in.computer_name;
1772         r2.in.logon_level       = r->in.logon_level;
1773         r2.in.logon             = r->in.logon;
1774         r2.in.validation_level  = r->in.validation_level;
1775         r2.in.flags             = r->in.flags;
1776         r2.out.validation       = r->out.validation;
1777         r2.out.authoritative    = r->out.authoritative;
1778         r2.out.flags            = r->out.flags;
1779
1780         status = _netr_LogonSamLogon_check(&r2);
1781         if (!NT_STATUS_IS_OK(status)) {
1782                 return status;
1783         }
1784
1785         become_root();
1786         status = netr_creds_server_step_check(p, p->mem_ctx,
1787                                               r->in.computer_name,
1788                                               r->in.credential,
1789                                               &return_authenticator,
1790                                               &creds);
1791         unbecome_root();
1792         if (!NT_STATUS_IS_OK(status)) {
1793                 return status;
1794         }
1795
1796         status = _netr_LogonSamLogon_base(p, &r2, creds);
1797
1798         *r->out.return_authenticator = return_authenticator;
1799
1800         return status;
1801 }
1802
1803 /*************************************************************************
1804  _netr_LogonSamLogon
1805  *************************************************************************/
1806
1807 NTSTATUS _netr_LogonSamLogon(struct pipes_struct *p,
1808                              struct netr_LogonSamLogon *r)
1809 {
1810         NTSTATUS status;
1811         struct netr_LogonSamLogonWithFlags r2;
1812         uint32_t flags = 0;
1813
1814         r2.in.server_name               = r->in.server_name;
1815         r2.in.computer_name             = r->in.computer_name;
1816         r2.in.credential                = r->in.credential;
1817         r2.in.logon_level               = r->in.logon_level;
1818         r2.in.logon                     = r->in.logon;
1819         r2.in.validation_level          = r->in.validation_level;
1820         r2.in.return_authenticator      = r->in.return_authenticator;
1821         r2.in.flags                     = &flags;
1822         r2.out.validation               = r->out.validation;
1823         r2.out.authoritative            = r->out.authoritative;
1824         r2.out.flags                    = &flags;
1825         r2.out.return_authenticator     = r->out.return_authenticator;
1826
1827         status = _netr_LogonSamLogonWithFlags(p, &r2);
1828
1829         return status;
1830 }
1831
1832 /*************************************************************************
1833  _netr_LogonSamLogonEx
1834  - no credential chaining. Map into net sam logon.
1835  *************************************************************************/
1836
1837 NTSTATUS _netr_LogonSamLogonEx(struct pipes_struct *p,
1838                                struct netr_LogonSamLogonEx *r)
1839 {
1840         NTSTATUS status;
1841         struct netlogon_creds_CredentialState *creds = NULL;
1842         struct loadparm_context *lp_ctx;
1843
1844         *r->out.authoritative = true;
1845
1846         status = _netr_LogonSamLogon_check(r);
1847         if (!NT_STATUS_IS_OK(status)) {
1848                 return status;
1849         }
1850
1851         /* Only allow this if the pipe is protected. */
1852         if (p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
1853                 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1854                         get_remote_machine_name() ));
1855                 return NT_STATUS_INVALID_PARAMETER;
1856         }
1857
1858         lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_helpers());
1859         if (lp_ctx == NULL) {
1860                 DEBUG(0, ("loadparm_init_s3 failed\n"));
1861                 return NT_STATUS_INTERNAL_ERROR;
1862         }
1863
1864         become_root();
1865         status = schannel_get_creds_state(p->mem_ctx, lp_ctx,
1866                                           r->in.computer_name, &creds);
1867         unbecome_root();
1868         talloc_unlink(p->mem_ctx, lp_ctx);
1869
1870         if (!NT_STATUS_IS_OK(status)) {
1871                 return status;
1872         }
1873
1874         status = _netr_LogonSamLogon_base(p, r, creds);
1875         TALLOC_FREE(creds);
1876
1877         return status;
1878 }
1879
1880 /*************************************************************************
1881  _ds_enum_dom_trusts
1882  *************************************************************************/
1883 #if 0   /* JERRY -- not correct */
1884  NTSTATUS _ds_enum_dom_trusts(struct pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1885                              DS_R_ENUM_DOM_TRUSTS *r_u)
1886 {
1887         NTSTATUS status = NT_STATUS_OK;
1888
1889         /* TODO: According to MSDN, the can only be executed against a
1890            DC or domain member running Windows 2000 or later.  Need
1891            to test against a standalone 2k server and see what it
1892            does.  A windows 2000 DC includes its own domain in the
1893            list.  --jerry */
1894
1895         return status;
1896 }
1897 #endif  /* JERRY */
1898
1899
1900 /****************************************************************
1901 ****************************************************************/
1902
1903 WERROR _netr_LogonUasLogon(struct pipes_struct *p,
1904                            struct netr_LogonUasLogon *r)
1905 {
1906         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1907         return WERR_NOT_SUPPORTED;
1908 }
1909
1910 /****************************************************************
1911 ****************************************************************/
1912
1913 WERROR _netr_LogonUasLogoff(struct pipes_struct *p,
1914                             struct netr_LogonUasLogoff *r)
1915 {
1916         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1917         return WERR_NOT_SUPPORTED;
1918 }
1919
1920 /****************************************************************
1921 ****************************************************************/
1922
1923 NTSTATUS _netr_DatabaseDeltas(struct pipes_struct *p,
1924                               struct netr_DatabaseDeltas *r)
1925 {
1926         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1927         return NT_STATUS_NOT_IMPLEMENTED;
1928 }
1929
1930 /****************************************************************
1931 ****************************************************************/
1932
1933 NTSTATUS _netr_DatabaseSync(struct pipes_struct *p,
1934                             struct netr_DatabaseSync *r)
1935 {
1936         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1937         return NT_STATUS_NOT_IMPLEMENTED;
1938 }
1939
1940 /****************************************************************
1941 ****************************************************************/
1942
1943 NTSTATUS _netr_AccountDeltas(struct pipes_struct *p,
1944                              struct netr_AccountDeltas *r)
1945 {
1946         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1947         return NT_STATUS_NOT_IMPLEMENTED;
1948 }
1949
1950 /****************************************************************
1951 ****************************************************************/
1952
1953 NTSTATUS _netr_AccountSync(struct pipes_struct *p,
1954                            struct netr_AccountSync *r)
1955 {
1956         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
1957         return NT_STATUS_NOT_IMPLEMENTED;
1958 }
1959
1960 /****************************************************************
1961 ****************************************************************/
1962
1963 static bool wb_getdcname(TALLOC_CTX *mem_ctx,
1964                          const char *domain,
1965                          const char **dcname,
1966                          uint32_t flags,
1967                          WERROR *werr)
1968 {
1969         wbcErr result;
1970         struct wbcDomainControllerInfo *dc_info = NULL;
1971
1972         result = wbcLookupDomainController(domain,
1973                                            flags,
1974                                            &dc_info);
1975         switch (result) {
1976         case WBC_ERR_SUCCESS:
1977                 break;
1978         case WBC_ERR_WINBIND_NOT_AVAILABLE:
1979                 return false;
1980         case WBC_ERR_DOMAIN_NOT_FOUND:
1981                 *werr = WERR_NO_SUCH_DOMAIN;
1982                 return true;
1983         default:
1984                 *werr = WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1985                 return true;
1986         }
1987
1988         *dcname = talloc_strdup(mem_ctx, dc_info->dc_name);
1989         wbcFreeMemory(dc_info);
1990         if (!*dcname) {
1991                 *werr = WERR_NOMEM;
1992                 return false;
1993         }
1994
1995         *werr = WERR_OK;
1996
1997         return true;
1998 }
1999
2000 /****************************************************************
2001  _netr_GetDcName
2002 ****************************************************************/
2003
2004 WERROR _netr_GetDcName(struct pipes_struct *p,
2005                        struct netr_GetDcName *r)
2006 {
2007         NTSTATUS status;
2008         WERROR werr;
2009         uint32_t flags;
2010         struct netr_DsRGetDCNameInfo *info;
2011         bool ret;
2012
2013         ret = wb_getdcname(p->mem_ctx,
2014                            r->in.domainname,
2015                            r->out.dcname,
2016                            WBC_LOOKUP_DC_IS_FLAT_NAME |
2017                            WBC_LOOKUP_DC_RETURN_FLAT_NAME |
2018                            WBC_LOOKUP_DC_PDC_REQUIRED,
2019                            &werr);
2020         if (ret == true) {
2021                 return werr;
2022         }
2023
2024         flags = DS_PDC_REQUIRED | DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
2025
2026         status = dsgetdcname(p->mem_ctx,
2027                              p->msg_ctx,
2028                              r->in.domainname,
2029                              NULL,
2030                              NULL,
2031                              flags,
2032                              &info);
2033         if (!NT_STATUS_IS_OK(status)) {
2034                 return ntstatus_to_werror(status);
2035         }
2036
2037         *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
2038         talloc_free(info);
2039         if (!*r->out.dcname) {
2040                 return WERR_NOMEM;
2041         }
2042
2043         return WERR_OK;
2044 }
2045
2046 /****************************************************************
2047  _netr_GetAnyDCName
2048 ****************************************************************/
2049
2050 WERROR _netr_GetAnyDCName(struct pipes_struct *p,
2051                           struct netr_GetAnyDCName *r)
2052 {
2053         NTSTATUS status;
2054         WERROR werr;
2055         uint32_t flags;
2056         struct netr_DsRGetDCNameInfo *info;
2057         bool ret;
2058
2059         ret = wb_getdcname(p->mem_ctx,
2060                            r->in.domainname,
2061                            r->out.dcname,
2062                            WBC_LOOKUP_DC_IS_FLAT_NAME |
2063                            WBC_LOOKUP_DC_RETURN_FLAT_NAME,
2064                            &werr);
2065         if (ret == true) {
2066                 return werr;
2067         }
2068
2069         flags = DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
2070
2071         status = dsgetdcname(p->mem_ctx,
2072                              p->msg_ctx,
2073                              r->in.domainname,
2074                              NULL,
2075                              NULL,
2076                              flags,
2077                              &info);
2078         if (!NT_STATUS_IS_OK(status)) {
2079                 return ntstatus_to_werror(status);
2080         }
2081
2082         *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
2083         talloc_free(info);
2084         if (!*r->out.dcname) {
2085                 return WERR_NOMEM;
2086         }
2087
2088         return WERR_OK;
2089 }
2090
2091 /****************************************************************
2092 ****************************************************************/
2093
2094 NTSTATUS _netr_DatabaseSync2(struct pipes_struct *p,
2095                              struct netr_DatabaseSync2 *r)
2096 {
2097         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2098         return NT_STATUS_NOT_IMPLEMENTED;
2099 }
2100
2101 /****************************************************************
2102 ****************************************************************/
2103
2104 NTSTATUS _netr_DatabaseRedo(struct pipes_struct *p,
2105                             struct netr_DatabaseRedo *r)
2106 {
2107         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2108         return NT_STATUS_NOT_IMPLEMENTED;
2109 }
2110
2111 /****************************************************************
2112 ****************************************************************/
2113
2114 WERROR _netr_DsRGetDCName(struct pipes_struct *p,
2115                           struct netr_DsRGetDCName *r)
2116 {
2117         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2118         return WERR_NOT_SUPPORTED;
2119 }
2120
2121 /****************************************************************
2122 ****************************************************************/
2123
2124 NTSTATUS _netr_LogonGetCapabilities(struct pipes_struct *p,
2125                                     struct netr_LogonGetCapabilities *r)
2126 {
2127         struct netlogon_creds_CredentialState *creds;
2128         NTSTATUS status;
2129
2130         become_root();
2131         status = netr_creds_server_step_check(p, p->mem_ctx,
2132                                               r->in.computer_name,
2133                                               r->in.credential,
2134                                               r->out.return_authenticator,
2135                                               &creds);
2136         unbecome_root();
2137         if (!NT_STATUS_IS_OK(status)) {
2138                 return status;
2139         }
2140
2141         if (r->in.query_level != 1) {
2142                 return NT_STATUS_NOT_SUPPORTED;
2143         }
2144
2145         r->out.capabilities->server_capabilities = creds->negotiate_flags;
2146
2147         return NT_STATUS_OK;
2148 }
2149
2150 /****************************************************************
2151 ****************************************************************/
2152
2153 WERROR _netr_NETRLOGONSETSERVICEBITS(struct pipes_struct *p,
2154                                      struct netr_NETRLOGONSETSERVICEBITS *r)
2155 {
2156         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2157         return WERR_NOT_SUPPORTED;
2158 }
2159
2160 /****************************************************************
2161 ****************************************************************/
2162
2163 WERROR _netr_LogonGetTrustRid(struct pipes_struct *p,
2164                               struct netr_LogonGetTrustRid *r)
2165 {
2166         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2167         return WERR_NOT_SUPPORTED;
2168 }
2169
2170 /****************************************************************
2171 ****************************************************************/
2172
2173 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(struct pipes_struct *p,
2174                                           struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
2175 {
2176         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2177         return WERR_NOT_SUPPORTED;
2178 }
2179
2180 /****************************************************************
2181 ****************************************************************/
2182
2183 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(struct pipes_struct *p,
2184                                           struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
2185 {
2186         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2187         return WERR_NOT_SUPPORTED;
2188 }
2189
2190 /****************************************************************
2191 ****************************************************************/
2192
2193 WERROR _netr_DsRGetDCNameEx(struct pipes_struct *p,
2194                             struct netr_DsRGetDCNameEx *r)
2195 {
2196         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2197         return WERR_NOT_SUPPORTED;
2198 }
2199
2200 /****************************************************************
2201 ****************************************************************/
2202
2203 WERROR _netr_DsRGetSiteName(struct pipes_struct *p,
2204                             struct netr_DsRGetSiteName *r)
2205 {
2206         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2207         return WERR_NOT_SUPPORTED;
2208 }
2209
2210 /****************************************************************
2211 ****************************************************************/
2212
2213 NTSTATUS _netr_LogonGetDomainInfo(struct pipes_struct *p,
2214                                   struct netr_LogonGetDomainInfo *r)
2215 {
2216         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2217         return NT_STATUS_NOT_IMPLEMENTED;
2218 }
2219
2220 /****************************************************************
2221 ****************************************************************/
2222
2223 NTSTATUS _netr_ServerPasswordGet(struct pipes_struct *p,
2224                                  struct netr_ServerPasswordGet *r)
2225 {
2226         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2227         return NT_STATUS_NOT_SUPPORTED;
2228 }
2229
2230 /****************************************************************
2231 ****************************************************************/
2232
2233 WERROR _netr_NETRLOGONSENDTOSAM(struct pipes_struct *p,
2234                                 struct netr_NETRLOGONSENDTOSAM *r)
2235 {
2236         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2237         return WERR_NOT_SUPPORTED;
2238 }
2239
2240 /****************************************************************
2241 ****************************************************************/
2242
2243 WERROR _netr_DsRAddressToSitenamesW(struct pipes_struct *p,
2244                                     struct netr_DsRAddressToSitenamesW *r)
2245 {
2246         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2247         return WERR_NOT_SUPPORTED;
2248 }
2249
2250 /****************************************************************
2251 ****************************************************************/
2252
2253 WERROR _netr_DsRGetDCNameEx2(struct pipes_struct *p,
2254                              struct netr_DsRGetDCNameEx2 *r)
2255 {
2256         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2257         return WERR_NOT_SUPPORTED;
2258 }
2259
2260 /****************************************************************
2261 ****************************************************************/
2262
2263 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(struct pipes_struct *p,
2264                                                  struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
2265 {
2266         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2267         return WERR_NOT_SUPPORTED;
2268 }
2269
2270 /****************************************************************
2271 ****************************************************************/
2272
2273 WERROR _netr_NetrEnumerateTrustedDomainsEx(struct pipes_struct *p,
2274                                            struct netr_NetrEnumerateTrustedDomainsEx *r)
2275 {
2276         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2277         return WERR_NOT_SUPPORTED;
2278 }
2279
2280 /****************************************************************
2281 ****************************************************************/
2282
2283 WERROR _netr_DsRAddressToSitenamesExW(struct pipes_struct *p,
2284                                       struct netr_DsRAddressToSitenamesExW *r)
2285 {
2286         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2287         return WERR_NOT_SUPPORTED;
2288 }
2289
2290 /****************************************************************
2291 ****************************************************************/
2292
2293 WERROR _netr_DsrGetDcSiteCoverageW(struct pipes_struct *p,
2294                                    struct netr_DsrGetDcSiteCoverageW *r)
2295 {
2296         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2297         return WERR_NOT_SUPPORTED;
2298 }
2299
2300 /****************************************************************
2301 ****************************************************************/
2302
2303 WERROR _netr_DsrEnumerateDomainTrusts(struct pipes_struct *p,
2304                                       struct netr_DsrEnumerateDomainTrusts *r)
2305 {
2306         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2307         return WERR_NOT_SUPPORTED;
2308 }
2309
2310 /****************************************************************
2311 ****************************************************************/
2312
2313 WERROR _netr_DsrDeregisterDNSHostRecords(struct pipes_struct *p,
2314                                          struct netr_DsrDeregisterDNSHostRecords *r)
2315 {
2316         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2317         return WERR_NOT_SUPPORTED;
2318 }
2319
2320 /****************************************************************
2321 ****************************************************************/
2322
2323 NTSTATUS _netr_ServerTrustPasswordsGet(struct pipes_struct *p,
2324                                        struct netr_ServerTrustPasswordsGet *r)
2325 {
2326         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2327         return NT_STATUS_NOT_IMPLEMENTED;
2328 }
2329
2330 /****************************************************************
2331 ****************************************************************/
2332
2333 static NTSTATUS fill_forest_trust_array(TALLOC_CTX *mem_ctx,
2334                                         struct lsa_ForestTrustInformation *info)
2335 {
2336         struct lsa_ForestTrustRecord *e;
2337         struct pdb_domain_info *dom_info;
2338         struct lsa_ForestTrustDomainInfo *domain_info;
2339         char **upn_suffixes = NULL;
2340         uint32_t num_suffixes = 0;
2341         uint32_t i = 0;
2342         NTSTATUS status;
2343
2344         dom_info = pdb_get_domain_info(mem_ctx);
2345         if (dom_info == NULL) {
2346                 return NT_STATUS_NO_MEMORY;
2347         }
2348
2349         info->count = 2;
2350
2351         become_root();
2352         status = pdb_enum_upn_suffixes(info, &num_suffixes, &upn_suffixes);
2353         unbecome_root();
2354         if (NT_STATUS_IS_OK(status) && (num_suffixes > 0)) {
2355                 info->count += num_suffixes;
2356         }
2357
2358         info->entries = talloc_array(info, struct lsa_ForestTrustRecord *, info->count);
2359         if (info->entries == NULL) {
2360                 return NT_STATUS_NO_MEMORY;
2361         }
2362
2363         e = talloc(info, struct lsa_ForestTrustRecord);
2364         if (e == NULL) {
2365                 return NT_STATUS_NO_MEMORY;
2366         }
2367
2368         e->flags = 0;
2369         e->type = LSA_FOREST_TRUST_TOP_LEVEL_NAME;
2370         e->time = 0; /* so far always 0 in trces. */
2371         e->forest_trust_data.top_level_name.string = talloc_steal(info,
2372                                                                   dom_info->dns_forest);
2373
2374         info->entries[0] = e;
2375
2376         if (num_suffixes > 0) {
2377                 for (i = 0; i < num_suffixes ; i++) {
2378                         e = talloc(info, struct lsa_ForestTrustRecord);
2379                         if (e == NULL) {
2380                                 return NT_STATUS_NO_MEMORY;
2381                         }
2382
2383                         e->flags = 0;
2384                         e->type = LSA_FOREST_TRUST_TOP_LEVEL_NAME;
2385                         e->time = 0; /* so far always 0 in traces. */
2386                         e->forest_trust_data.top_level_name.string = upn_suffixes[i];
2387                         info->entries[1 + i] = e;
2388                 }
2389         }
2390
2391         e = talloc(info, struct lsa_ForestTrustRecord);
2392         if (e == NULL) {
2393                 return NT_STATUS_NO_MEMORY;
2394         }
2395
2396         /* TODO: check if disabled and set flags accordingly */
2397         e->flags = 0;
2398         e->type = LSA_FOREST_TRUST_DOMAIN_INFO;
2399         e->time = 0; /* so far always 0 in traces. */
2400
2401         domain_info = &e->forest_trust_data.domain_info;
2402         domain_info->domain_sid = dom_sid_dup(info, &dom_info->sid);
2403
2404         domain_info->dns_domain_name.string = talloc_steal(info,
2405                                                            dom_info->dns_domain);
2406         domain_info->netbios_domain_name.string = talloc_steal(info,
2407                                                                dom_info->name);
2408
2409         info->entries[info->count - 1] = e;
2410
2411         return NT_STATUS_OK;
2412 }
2413
2414 /****************************************************************
2415 ****************************************************************/
2416
2417 WERROR _netr_DsRGetForestTrustInformation(struct pipes_struct *p,
2418                                           struct netr_DsRGetForestTrustInformation *r)
2419 {
2420         NTSTATUS status;
2421         struct lsa_ForestTrustInformation *info, **info_ptr;
2422
2423         if (!(p->pipe_bound && (p->auth.auth_type != DCERPC_AUTH_TYPE_NONE)
2424                        && (p->auth.auth_level != DCERPC_AUTH_LEVEL_NONE))) {
2425                 p->fault_state = DCERPC_FAULT_ACCESS_DENIED;
2426                 return WERR_ACCESS_DENIED;
2427         }
2428
2429         if (r->in.flags & (~DS_GFTI_UPDATE_TDO)) {
2430                 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2431                 return WERR_INVALID_FLAGS;
2432         }
2433
2434         if ((r->in.flags & DS_GFTI_UPDATE_TDO) && (lp_server_role() != ROLE_DOMAIN_PDC)) {
2435                 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2436                 return WERR_NERR_NOTPRIMARY;
2437         }
2438
2439         if ((r->in.trusted_domain_name == NULL) && (r->in.flags & DS_GFTI_UPDATE_TDO)) {
2440                 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2441                 return WERR_INVALID_PARAMETER;
2442         }
2443
2444         /* retrieve forest trust information and stop further processing */
2445         if (r->in.trusted_domain_name == NULL) {
2446                 info_ptr = talloc(p->mem_ctx, struct lsa_ForestTrustInformation *);
2447                 if (info_ptr == NULL) {
2448                         p->fault_state = DCERPC_FAULT_CANT_PERFORM;
2449                         return WERR_NOMEM;
2450                 }
2451                 info = talloc_zero(info_ptr, struct lsa_ForestTrustInformation);
2452                 if (info == NULL) {
2453                         p->fault_state = DCERPC_FAULT_CANT_PERFORM;
2454                         return WERR_NOMEM;
2455                 }
2456
2457                 /* Fill forest trust information and expand UPN suffixes list */
2458                 status = fill_forest_trust_array(p->mem_ctx, info);
2459                 if (!NT_STATUS_IS_OK(status)) {
2460                         p->fault_state = DCERPC_FAULT_CANT_PERFORM;
2461                         return WERR_NOMEM;
2462                 }
2463
2464                 *info_ptr = info;
2465                 r->out.forest_trust_info = info_ptr;
2466
2467                 return WERR_OK;
2468
2469         }
2470
2471         /* TODO: implement remaining parts of DsrGetForestTrustInformation (opnum 43)
2472          *       when trusted_domain_name is not NULL */
2473
2474         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2475         return WERR_NOT_SUPPORTED;
2476 }
2477
2478 /****************************************************************
2479  _netr_GetForestTrustInformation
2480 ****************************************************************/
2481
2482 NTSTATUS _netr_GetForestTrustInformation(struct pipes_struct *p,
2483                                          struct netr_GetForestTrustInformation *r)
2484 {
2485         NTSTATUS status;
2486         struct netlogon_creds_CredentialState *creds;
2487         struct lsa_ForestTrustInformation *info, **info_ptr;
2488
2489         /* TODO: check server name */
2490
2491         become_root();
2492         status = netr_creds_server_step_check(p, p->mem_ctx,
2493                                               r->in.computer_name,
2494                                               r->in.credential,
2495                                               r->out.return_authenticator,
2496                                               &creds);
2497         unbecome_root();
2498         if (!NT_STATUS_IS_OK(status)) {
2499                 return status;
2500         }
2501
2502         if ((creds->secure_channel_type != SEC_CHAN_DNS_DOMAIN) &&
2503             (creds->secure_channel_type != SEC_CHAN_DOMAIN)) {
2504                 return NT_STATUS_NOT_IMPLEMENTED;
2505         }
2506
2507         info_ptr = talloc(p->mem_ctx, struct lsa_ForestTrustInformation *);
2508         if (!info_ptr) {
2509                 return NT_STATUS_NO_MEMORY;
2510         }
2511         info = talloc_zero(info_ptr, struct lsa_ForestTrustInformation);
2512         if (!info) {
2513                 return NT_STATUS_NO_MEMORY;
2514         }
2515
2516         /* Fill forest trust information, do expand UPN suffixes list */
2517         status = fill_forest_trust_array(p->mem_ctx, info);
2518         if (!NT_STATUS_IS_OK(status)) {
2519                 return status;
2520         }
2521
2522         *info_ptr = info;
2523         r->out.forest_trust_info = info_ptr;
2524
2525         return NT_STATUS_OK;
2526 }
2527
2528 /****************************************************************
2529 ****************************************************************/
2530
2531 static NTSTATUS get_password_from_trustAuth(TALLOC_CTX *mem_ctx,
2532                                             const DATA_BLOB *trustAuth_blob,
2533                                             struct netlogon_creds_CredentialState *creds,
2534                                             struct samr_Password *current_pw_enc,
2535                                             struct samr_Password *previous_pw_enc)
2536 {
2537         enum ndr_err_code ndr_err;
2538         struct trustAuthInOutBlob trustAuth;
2539
2540         ndr_err = ndr_pull_struct_blob_all(trustAuth_blob, mem_ctx, &trustAuth,
2541                                            (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2542         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2543                 return NT_STATUS_UNSUCCESSFUL;
2544         }
2545
2546         if (trustAuth.count != 0 && trustAuth.current.count != 0 &&
2547             trustAuth.current.array[0].AuthType == TRUST_AUTH_TYPE_CLEAR) {
2548                 mdfour(current_pw_enc->hash,
2549                        trustAuth.current.array[0].AuthInfo.clear.password,
2550                        trustAuth.current.array[0].AuthInfo.clear.size);
2551                 netlogon_creds_des_encrypt(creds, current_pw_enc);
2552         } else {
2553                 return NT_STATUS_UNSUCCESSFUL;
2554         }
2555
2556
2557         if (trustAuth.previous.count != 0 &&
2558             trustAuth.previous.array[0].AuthType == TRUST_AUTH_TYPE_CLEAR) {
2559                 mdfour(previous_pw_enc->hash,
2560                        trustAuth.previous.array[0].AuthInfo.clear.password,
2561                        trustAuth.previous.array[0].AuthInfo.clear.size);
2562                 netlogon_creds_des_encrypt(creds, previous_pw_enc);
2563         } else {
2564                 ZERO_STRUCTP(previous_pw_enc);
2565         }
2566
2567         return NT_STATUS_OK;
2568 }
2569
2570 /****************************************************************
2571  _netr_ServerGetTrustInfo
2572 ****************************************************************/
2573
2574 NTSTATUS _netr_ServerGetTrustInfo(struct pipes_struct *p,
2575                                   struct netr_ServerGetTrustInfo *r)
2576 {
2577         NTSTATUS status;
2578         struct netlogon_creds_CredentialState *creds;
2579         char *account_name;
2580         size_t account_name_last;
2581         bool trusted;
2582         struct netr_TrustInfo *trust_info;
2583         struct pdb_trusted_domain *td;
2584
2585         /* TODO: check server name */
2586
2587         become_root();
2588         status = netr_creds_server_step_check(p, p->mem_ctx,
2589                                               r->in.computer_name,
2590                                               r->in.credential,
2591                                               r->out.return_authenticator,
2592                                               &creds);
2593         unbecome_root();
2594         if (!NT_STATUS_IS_OK(status)) {
2595                 return status;
2596         }
2597
2598         account_name = talloc_strdup(p->mem_ctx, r->in.account_name);
2599         if (account_name == NULL) {
2600                 return NT_STATUS_NO_MEMORY;
2601         }
2602
2603         account_name_last = strlen(account_name);
2604         if (account_name_last == 0) {
2605                 return NT_STATUS_INVALID_PARAMETER;
2606         }
2607         account_name_last--;
2608         if (account_name[account_name_last] == '.') {
2609                 account_name[account_name_last] = '\0';
2610         }
2611
2612         if ((creds->secure_channel_type != SEC_CHAN_DNS_DOMAIN) &&
2613             (creds->secure_channel_type != SEC_CHAN_DOMAIN)) {
2614                 trusted = false;
2615         } else {
2616                 trusted = true;
2617         }
2618
2619
2620         if (trusted) {
2621                 account_name_last = strlen(account_name);
2622                 if (account_name_last == 0) {
2623                         return NT_STATUS_INVALID_PARAMETER;
2624                 }
2625                 account_name_last--;
2626                 if (account_name[account_name_last] == '$') {
2627                         account_name[account_name_last] = '\0';
2628                 }
2629
2630                 status = pdb_get_trusted_domain(p->mem_ctx, account_name, &td);
2631                 if (!NT_STATUS_IS_OK(status)) {
2632                         return status;
2633                 }
2634
2635                 if (r->out.trust_info != NULL) {
2636                         trust_info = talloc_zero(p->mem_ctx, struct netr_TrustInfo);
2637                         if (trust_info == NULL) {
2638                                 return NT_STATUS_NO_MEMORY;
2639                         }
2640                         trust_info->count = 1;
2641
2642                         trust_info->data = talloc_array(trust_info, uint32_t, 1);
2643                         if (trust_info->data == NULL) {
2644                                 return NT_STATUS_NO_MEMORY;
2645                         }
2646                         trust_info->data[0] = td->trust_attributes;
2647
2648                         *r->out.trust_info = trust_info;
2649                 }
2650
2651                 if (td->trust_auth_incoming.data == NULL) {
2652                         return NT_STATUS_INVALID_PARAMETER;
2653                 }
2654
2655                 status = get_password_from_trustAuth(p->mem_ctx,
2656                                                      &td->trust_auth_incoming,
2657                                                      creds,
2658                                                      r->out.new_owf_password,
2659                                                      r->out.old_owf_password);
2660
2661                 if (!NT_STATUS_IS_OK(status)) {
2662                         return status;
2663                 }
2664
2665         } else {
2666 /* TODO: look for machine password */
2667                 ZERO_STRUCTP(r->out.new_owf_password);
2668                 ZERO_STRUCTP(r->out.old_owf_password);
2669
2670                 return NT_STATUS_NOT_IMPLEMENTED;
2671         }
2672
2673         return NT_STATUS_OK;
2674 }
2675
2676 /****************************************************************
2677 ****************************************************************/
2678
2679 NTSTATUS _netr_Unused47(struct pipes_struct *p,
2680                         struct netr_Unused47 *r)
2681 {
2682         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2683         return NT_STATUS_NOT_IMPLEMENTED;
2684 }
2685
2686 /****************************************************************
2687 ****************************************************************/
2688
2689 NTSTATUS _netr_DsrUpdateReadOnlyServerDnsRecords(struct pipes_struct *p,
2690                                                  struct netr_DsrUpdateReadOnlyServerDnsRecords *r)
2691 {
2692         p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
2693         return NT_STATUS_NOT_IMPLEMENTED;
2694 }