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