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