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