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