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