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