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