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