s3:cli_netlogon: make sure rpccli_connect_netlogon only returns NT_STATUS_OK on success
[metze/samba/wip.git] / source3 / rpc_client / cli_netlogon.c
1 /*
2    Unix SMB/CIFS implementation.
3    NT Domain Authentication SMB / MSRPC client
4    Copyright (C) Andrew Tridgell 1992-2000
5    Copyright (C) Jeremy Allison                    1998.
6    Largely re-written by Jeremy Allison (C)        2005.
7    Copyright (C) Guenther Deschner                 2008.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "libsmb/libsmb.h"
26 #include "rpc_client/rpc_client.h"
27 #include "rpc_client/cli_pipe.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #include "../libcli/auth/netlogon_creds_cli.h"
30 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
31 #include "../librpc/gen_ndr/schannel.h"
32 #include "rpc_client/cli_netlogon.h"
33 #include "rpc_client/init_netlogon.h"
34 #include "rpc_client/util_netlogon.h"
35 #include "../libcli/security/security.h"
36 #include "lib/param/param.h"
37 #include "libcli/smb/smbXcli_base.h"
38 #include "dbwrap/dbwrap.h"
39 #include "dbwrap/dbwrap_open.h"
40 #include "util_tdb.h"
41
42
43 NTSTATUS rpccli_pre_open_netlogon_creds(void)
44 {
45         static bool already_open = false;
46         TALLOC_CTX *frame;
47         struct loadparm_context *lp_ctx;
48         char *fname;
49         struct db_context *global_db;
50         NTSTATUS status;
51
52         if (already_open) {
53                 return NT_STATUS_OK;
54         }
55
56         frame = talloc_stackframe();
57
58         lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
59         if (lp_ctx == NULL) {
60                 TALLOC_FREE(frame);
61                 return NT_STATUS_NO_MEMORY;
62         }
63
64         fname = lpcfg_private_db_path(frame, lp_ctx, "netlogon_creds_cli");
65         if (fname == NULL) {
66                 TALLOC_FREE(frame);
67                 return NT_STATUS_NO_MEMORY;
68         }
69
70         global_db = db_open(frame, fname,
71                             0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
72                             O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_2,
73                             DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS);
74         if (global_db == NULL) {
75                 TALLOC_FREE(frame);
76                 return NT_STATUS_NO_MEMORY;
77         }
78
79         status = netlogon_creds_cli_set_global_db(&global_db);
80         TALLOC_FREE(frame);
81         if (!NT_STATUS_IS_OK(status)) {
82                 return status;
83         }
84
85         already_open = true;
86         return NT_STATUS_OK;
87 }
88
89 static NTSTATUS rpccli_create_netlogon_creds(
90         const char *server_computer,
91         const char *server_netbios_domain,
92         const char *server_dns_domain,
93         const char *client_account,
94         enum netr_SchannelType sec_chan_type,
95         struct messaging_context *msg_ctx,
96         TALLOC_CTX *mem_ctx,
97         struct netlogon_creds_cli_context **netlogon_creds)
98 {
99         TALLOC_CTX *frame = talloc_stackframe();
100         struct loadparm_context *lp_ctx;
101         NTSTATUS status;
102
103         status = rpccli_pre_open_netlogon_creds();
104         if (!NT_STATUS_IS_OK(status)) {
105                 TALLOC_FREE(frame);
106                 return status;
107         }
108
109         lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
110         if (lp_ctx == NULL) {
111                 TALLOC_FREE(frame);
112                 return NT_STATUS_NO_MEMORY;
113         }
114         status = netlogon_creds_cli_context_global(lp_ctx,
115                                                    msg_ctx,
116                                                    client_account,
117                                                    sec_chan_type,
118                                                    server_computer,
119                                                    server_netbios_domain,
120                                                    server_dns_domain,
121                                                    mem_ctx, netlogon_creds);
122         TALLOC_FREE(frame);
123         if (!NT_STATUS_IS_OK(status)) {
124                 return status;
125         }
126
127         return NT_STATUS_OK;
128 }
129
130 NTSTATUS rpccli_create_netlogon_creds_ctx(
131         struct cli_credentials *creds,
132         const char *server_computer,
133         struct messaging_context *msg_ctx,
134         TALLOC_CTX *mem_ctx,
135         struct netlogon_creds_cli_context **creds_ctx)
136 {
137         enum netr_SchannelType sec_chan_type;
138         const char *server_netbios_domain;
139         const char *server_dns_domain;
140         const char *client_account;
141
142         sec_chan_type = cli_credentials_get_secure_channel_type(creds);
143         client_account = cli_credentials_get_username(creds);
144         server_netbios_domain = cli_credentials_get_domain(creds);
145         server_dns_domain = cli_credentials_get_realm(creds);
146
147         return rpccli_create_netlogon_creds(server_computer,
148                                             server_netbios_domain,
149                                             server_dns_domain,
150                                             client_account,
151                                             sec_chan_type,
152                                             msg_ctx, mem_ctx,
153                                             creds_ctx);
154 }
155
156 NTSTATUS rpccli_setup_netlogon_creds_locked(
157         struct cli_state *cli,
158         enum dcerpc_transport_t transport,
159         struct netlogon_creds_cli_context *creds_ctx,
160         bool force_reauth,
161         struct cli_credentials *cli_creds,
162         uint32_t *negotiate_flags)
163 {
164         TALLOC_CTX *frame = talloc_stackframe();
165         struct rpc_pipe_client *netlogon_pipe = NULL;
166         struct netlogon_creds_CredentialState *creds = NULL;
167         uint8_t num_nt_hashes = 0;
168         const struct samr_Password *nt_hashes[2] = { NULL, NULL };
169         uint8_t idx_nt_hashes = 0;
170         NTSTATUS status;
171
172         status = netlogon_creds_cli_get(creds_ctx, frame, &creds);
173         if (NT_STATUS_IS_OK(status)) {
174                 const char *action = "using";
175
176                 if (force_reauth) {
177                         action = "overwrite";
178                 }
179
180                 DEBUG(5,("%s: %s cached netlogon_creds cli[%s/%s] to %s\n",
181                          __FUNCTION__, action,
182                          creds->account_name, creds->computer_name,
183                          smbXcli_conn_remote_name(cli->conn)));
184                 if (!force_reauth) {
185                         goto done;
186                 }
187                 TALLOC_FREE(creds);
188         }
189
190         nt_hashes[0] = cli_credentials_get_nt_hash(cli_creds, talloc_tos());
191         if (nt_hashes[0] == NULL) {
192                 TALLOC_FREE(frame);
193                 return NT_STATUS_NO_MEMORY;
194         }
195         num_nt_hashes = 1;
196
197         nt_hashes[1] = cli_credentials_get_old_nt_hash(cli_creds,
198                                                        talloc_tos());
199         if (nt_hashes[1] != NULL) {
200                 num_nt_hashes = 2;
201         }
202
203         status = cli_rpc_pipe_open_noauth_transport(cli,
204                                                     transport,
205                                                     &ndr_table_netlogon,
206                                                     &netlogon_pipe);
207         if (!NT_STATUS_IS_OK(status)) {
208                 DEBUG(5,("%s: failed to open noauth netlogon connection to %s - %s\n",
209                          __FUNCTION__,
210                          smbXcli_conn_remote_name(cli->conn),
211                          nt_errstr(status)));
212                 TALLOC_FREE(frame);
213                 return status;
214         }
215         talloc_steal(frame, netlogon_pipe);
216
217         status = netlogon_creds_cli_auth(creds_ctx,
218                                          netlogon_pipe->binding_handle,
219                                          num_nt_hashes,
220                                          nt_hashes,
221                                          &idx_nt_hashes);
222         if (!NT_STATUS_IS_OK(status)) {
223                 TALLOC_FREE(frame);
224                 return status;
225         }
226
227         status = netlogon_creds_cli_get(creds_ctx, frame, &creds);
228         if (!NT_STATUS_IS_OK(status)) {
229                 TALLOC_FREE(frame);
230                 return NT_STATUS_INTERNAL_ERROR;
231         }
232
233         DEBUG(5,("%s: using new netlogon_creds cli[%s/%s] to %s\n",
234                  __FUNCTION__,
235                  creds->account_name, creds->computer_name,
236                  smbXcli_conn_remote_name(cli->conn)));
237
238 done:
239         if (negotiate_flags != NULL) {
240                 *negotiate_flags = creds->negotiate_flags;
241         }
242
243         TALLOC_FREE(frame);
244         return NT_STATUS_OK;
245 }
246
247 NTSTATUS rpccli_setup_netlogon_creds(
248         struct cli_state *cli,
249         enum dcerpc_transport_t transport,
250         struct netlogon_creds_cli_context *creds_ctx,
251         bool force_reauth,
252         struct cli_credentials *cli_creds)
253 {
254         TALLOC_CTX *frame = talloc_stackframe();
255         struct netlogon_creds_cli_lck *lck;
256         NTSTATUS status;
257
258         status = netlogon_creds_cli_lck(
259                 creds_ctx, NETLOGON_CREDS_CLI_LCK_EXCLUSIVE,
260                 frame, &lck);
261         if (!NT_STATUS_IS_OK(status)) {
262                 DBG_WARNING("netlogon_creds_cli_lck failed: %s\n",
263                             nt_errstr(status));
264                 TALLOC_FREE(frame);
265                 return status;
266         }
267
268         status = rpccli_setup_netlogon_creds_locked(
269                 cli, transport, creds_ctx, force_reauth, cli_creds, NULL);
270
271         TALLOC_FREE(frame);
272
273         return status;
274 }
275
276 NTSTATUS rpccli_connect_netlogon(
277         struct cli_state *cli,
278         enum dcerpc_transport_t transport,
279         struct netlogon_creds_cli_context *creds_ctx,
280         bool force_reauth,
281         struct cli_credentials *trust_creds,
282         struct rpc_pipe_client **_rpccli)
283 {
284         TALLOC_CTX *frame = talloc_stackframe();
285         struct netlogon_creds_CredentialState *creds = NULL;
286         enum netlogon_creds_cli_lck_type lck_type;
287         enum netr_SchannelType sec_chan_type;
288         struct netlogon_creds_cli_lck *lck;
289         uint32_t negotiate_flags;
290         uint8_t found_session_key[16] = {0};
291         bool found_existing_creds = false;
292         bool do_serverauth;
293         struct rpc_pipe_client *rpccli;
294         NTSTATUS status;
295
296 again:
297
298         /*
299          * See whether we can use existing netlogon_creds or
300          * whether we have to serverauthenticate.
301          */
302         status = netlogon_creds_cli_get(creds_ctx, frame, &creds);
303
304         if (NT_STATUS_IS_OK(status)) {
305                 int cmp = memcmp(found_session_key,
306                                  creds->session_key,
307                                  sizeof(found_session_key));
308                 found_existing_creds = (cmp != 0);
309
310                 memcpy(found_session_key,
311                        creds->session_key,
312                        sizeof(found_session_key));
313
314                 TALLOC_FREE(creds);
315         }
316
317         lck_type = (force_reauth || !found_existing_creds) ?
318                 NETLOGON_CREDS_CLI_LCK_EXCLUSIVE :
319                 NETLOGON_CREDS_CLI_LCK_SHARED;
320
321         status = netlogon_creds_cli_lck(creds_ctx, lck_type, frame, &lck);
322         if (!NT_STATUS_IS_OK(status)) {
323                 DBG_DEBUG("netlogon_creds_cli_lck failed: %s\n",
324                           nt_errstr(status));
325                 goto fail;
326         }
327
328         if (!found_existing_creds) {
329                 /*
330                  * Try to find creds under the lock again. Someone
331                  * else might have done it for us.
332                  */
333                 status = netlogon_creds_cli_get(creds_ctx, frame, &creds);
334
335                 if (NT_STATUS_IS_OK(status)) {
336                         int cmp = memcmp(found_session_key,
337                                          creds->session_key,
338                                          sizeof(found_session_key));
339                         found_existing_creds = (cmp != 0);
340
341                         memcpy(found_session_key, creds->session_key,
342                                sizeof(found_session_key));
343
344                         TALLOC_FREE(creds);
345                 }
346         }
347
348         do_serverauth = force_reauth || !found_existing_creds;
349
350         if (!do_serverauth) {
351                 /*
352                  * Do the quick schannel bind without a reauth
353                  */
354                 status = cli_rpc_pipe_open_bind_schannel(
355                         cli, &ndr_table_netlogon, transport, creds_ctx,
356                         &rpccli);
357                 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_ACCESS_DENIED)) {
358                         DBG_DEBUG("Retrying with serverauthenticate\n");
359                         TALLOC_FREE(lck);
360                         goto again;
361                 }
362                 if (!NT_STATUS_IS_OK(status)) {
363                         DBG_DEBUG("cli_rpc_pipe_open_bind_schannel "
364                                   "failed: %s\n", nt_errstr(status));
365                         goto fail;
366                 }
367                 goto done;
368         }
369
370         if (cli_credentials_is_anonymous(trust_creds)) {
371                 DBG_WARNING("get_trust_credential for %s only gave anonymous,"
372                             "unable to negotiate NETLOGON credentials\n",
373                             netlogon_creds_cli_debug_string(
374                                     creds_ctx, frame));
375                 status = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
376                 goto fail;
377         }
378
379         sec_chan_type = cli_credentials_get_secure_channel_type(trust_creds);
380         if (sec_chan_type == SEC_CHAN_NULL) {
381                 if (transport == NCACN_IP_TCP) {
382                         DBG_NOTICE("secure_channel_type gave SEC_CHAN_NULL "
383                                    "for %s, deny NCACN_IP_TCP and let the "
384                                    "caller fallback to NCACN_NP.\n",
385                                    netlogon_creds_cli_debug_string(
386                                            creds_ctx, frame));
387                         status = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
388                         goto fail;
389                 }
390
391                 DBG_NOTICE("get_secure_channel_type gave SEC_CHAN_NULL "
392                            "for %s, fallback to noauth on NCACN_NP.\n",
393                            netlogon_creds_cli_debug_string(
394                                    creds_ctx, frame));
395
396                 TALLOC_FREE(lck);
397
398                 status = cli_rpc_pipe_open_noauth_transport(
399                         cli, transport, &ndr_table_netlogon, &rpccli);
400                 if (!NT_STATUS_IS_OK(status)) {
401                         DBG_DEBUG("cli_rpc_pipe_open_noauth_transport "
402                                   "failed: %s\n", nt_errstr(status));
403                         goto fail;
404                 }
405                 goto done;
406         }
407
408         status = rpccli_setup_netlogon_creds_locked(
409                 cli, transport, creds_ctx, true, trust_creds,
410                 &negotiate_flags);
411         if (!NT_STATUS_IS_OK(status)) {
412                 DBG_DEBUG("rpccli_setup_netlogon_creds failed for %s, "
413                           "unable to setup NETLOGON credentials: %s\n",
414                           netlogon_creds_cli_debug_string(
415                                   creds_ctx, frame),
416                           nt_errstr(status));
417                 goto fail;
418         }
419
420         if (!(negotiate_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
421                 if (lp_winbind_sealed_pipes() || lp_require_strong_key()) {
422                         status = NT_STATUS_DOWNGRADE_DETECTED;
423                         DBG_WARNING("Unwilling to make connection to %s"
424                                     "without connection level security, "
425                                     "must set 'winbind sealed pipes = false'"
426                                     " and 'require strong key = false' "
427                                     "to proceed: %s\n",
428                                     netlogon_creds_cli_debug_string(
429                                             creds_ctx, frame),
430                                     nt_errstr(status));
431                         goto fail;
432                 }
433
434                 status = cli_rpc_pipe_open_noauth_transport(
435                         cli, transport, &ndr_table_netlogon, &rpccli);
436                 if (!NT_STATUS_IS_OK(status)) {
437                         DBG_DEBUG("cli_rpc_pipe_open_noauth_transport "
438                                   "failed: %s\n", nt_errstr(status));
439                         goto fail;
440                 }
441                 goto done;
442         }
443
444         status = cli_rpc_pipe_open_bind_schannel(
445                 cli, &ndr_table_netlogon, transport, creds_ctx, &rpccli);
446         if (!NT_STATUS_IS_OK(status)) {
447                 DBG_DEBUG("cli_rpc_pipe_open_bind_schannel "
448                           "failed: %s\n", nt_errstr(status));
449                 goto fail;
450         }
451
452         status = netlogon_creds_cli_check(creds_ctx, rpccli->binding_handle,
453                                           NULL);
454         if (!NT_STATUS_IS_OK(status)) {
455                 DBG_WARNING("netlogon_creds_cli_check failed: %s\n",
456                             nt_errstr(status));
457                 goto fail;
458         }
459
460 done:
461         *_rpccli = rpccli;
462         status = NT_STATUS_OK;
463 fail:
464         ZERO_STRUCT(found_session_key);
465         TALLOC_FREE(lck);
466         TALLOC_FREE(frame);
467         return status;
468 }
469
470 static NTSTATUS map_validation_to_info3(TALLOC_CTX *mem_ctx,
471                                         uint16_t validation_level,
472                                         union netr_Validation *validation,
473                                         struct netr_SamInfo3 **info3_p)
474 {
475         struct netr_SamInfo3 *info3;
476         NTSTATUS status;
477
478         if (validation == NULL) {
479                 return NT_STATUS_INVALID_PARAMETER;
480         }
481
482         switch (validation_level) {
483         case 3:
484                 if (validation->sam3 == NULL) {
485                         return NT_STATUS_INVALID_PARAMETER;
486                 }
487
488                 info3 = talloc_move(mem_ctx, &validation->sam3);
489                 break;
490         case 6:
491                 if (validation->sam6 == NULL) {
492                         return NT_STATUS_INVALID_PARAMETER;
493                 }
494
495                 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
496                 if (info3 == NULL) {
497                         return NT_STATUS_NO_MEMORY;
498                 }
499                 status = copy_netr_SamBaseInfo(info3, &validation->sam6->base, &info3->base);
500                 if (!NT_STATUS_IS_OK(status)) {
501                         TALLOC_FREE(info3);
502                         return status;
503                 }
504
505                 info3->sidcount = validation->sam6->sidcount;
506                 info3->sids = talloc_move(info3, &validation->sam6->sids);
507                 break;
508         default:
509                 return NT_STATUS_BAD_VALIDATION_CLASS;
510         }
511
512         *info3_p = info3;
513
514         return NT_STATUS_OK;
515 }
516
517 /* Logon domain user */
518
519 NTSTATUS rpccli_netlogon_password_logon(
520         struct netlogon_creds_cli_context *creds_ctx,
521         struct dcerpc_binding_handle *binding_handle,
522         TALLOC_CTX *mem_ctx,
523         uint32_t logon_parameters,
524         const char *domain,
525         const char *username,
526         const char *password,
527         const char *workstation,
528         enum netr_LogonInfoClass logon_type,
529         uint8_t *authoritative,
530         uint32_t *flags,
531         struct netr_SamInfo3 **info3)
532 {
533         TALLOC_CTX *frame = talloc_stackframe();
534         NTSTATUS status;
535         union netr_LogonLevel *logon;
536         uint16_t validation_level = 0;
537         union netr_Validation *validation = NULL;
538         char *workstation_slash = NULL;
539
540         logon = talloc_zero(frame, union netr_LogonLevel);
541         if (logon == NULL) {
542                 TALLOC_FREE(frame);
543                 return NT_STATUS_NO_MEMORY;
544         }
545
546         if (workstation == NULL) {
547                 workstation = lp_netbios_name();
548         }
549
550         workstation_slash = talloc_asprintf(frame, "\\\\%s", workstation);
551         if (workstation_slash == NULL) {
552                 TALLOC_FREE(frame);
553                 return NT_STATUS_NO_MEMORY;
554         }
555
556         /* Initialise input parameters */
557
558         switch (logon_type) {
559         case NetlogonInteractiveInformation: {
560
561                 struct netr_PasswordInfo *password_info;
562
563                 struct samr_Password lmpassword;
564                 struct samr_Password ntpassword;
565
566                 password_info = talloc_zero(frame, struct netr_PasswordInfo);
567                 if (password_info == NULL) {
568                         TALLOC_FREE(frame);
569                         return NT_STATUS_NO_MEMORY;
570                 }
571
572                 nt_lm_owf_gen(password, ntpassword.hash, lmpassword.hash);
573
574                 password_info->identity_info.domain_name.string         = domain;
575                 password_info->identity_info.parameter_control          = logon_parameters;
576                 password_info->identity_info.logon_id_low               = 0xdead;
577                 password_info->identity_info.logon_id_high              = 0xbeef;
578                 password_info->identity_info.account_name.string        = username;
579                 password_info->identity_info.workstation.string         = workstation_slash;
580
581                 password_info->lmpassword = lmpassword;
582                 password_info->ntpassword = ntpassword;
583
584                 logon->password = password_info;
585
586                 break;
587         }
588         case NetlogonNetworkInformation: {
589                 struct netr_NetworkInfo *network_info;
590                 uint8_t chal[8];
591                 unsigned char local_lm_response[24];
592                 unsigned char local_nt_response[24];
593                 struct netr_ChallengeResponse lm;
594                 struct netr_ChallengeResponse nt;
595
596                 ZERO_STRUCT(lm);
597                 ZERO_STRUCT(nt);
598
599                 network_info = talloc_zero(frame, struct netr_NetworkInfo);
600                 if (network_info == NULL) {
601                         TALLOC_FREE(frame);
602                         return NT_STATUS_NO_MEMORY;
603                 }
604
605                 generate_random_buffer(chal, 8);
606
607                 SMBencrypt(password, chal, local_lm_response);
608                 SMBNTencrypt(password, chal, local_nt_response);
609
610                 lm.length = 24;
611                 lm.data = local_lm_response;
612
613                 nt.length = 24;
614                 nt.data = local_nt_response;
615
616                 network_info->identity_info.domain_name.string          = domain;
617                 network_info->identity_info.parameter_control           = logon_parameters;
618                 network_info->identity_info.logon_id_low                = 0xdead;
619                 network_info->identity_info.logon_id_high               = 0xbeef;
620                 network_info->identity_info.account_name.string         = username;
621                 network_info->identity_info.workstation.string          = workstation_slash;
622
623                 memcpy(network_info->challenge, chal, 8);
624                 network_info->nt = nt;
625                 network_info->lm = lm;
626
627                 logon->network = network_info;
628
629                 break;
630         }
631         default:
632                 DEBUG(0, ("switch value %d not supported\n",
633                         logon_type));
634                 TALLOC_FREE(frame);
635                 return NT_STATUS_INVALID_INFO_CLASS;
636         }
637
638         status = netlogon_creds_cli_LogonSamLogon(creds_ctx,
639                                                   binding_handle,
640                                                   logon_type,
641                                                   logon,
642                                                   frame,
643                                                   &validation_level,
644                                                   &validation,
645                                                   authoritative,
646                                                   flags);
647         if (!NT_STATUS_IS_OK(status)) {
648                 TALLOC_FREE(frame);
649                 return status;
650         }
651
652         status = map_validation_to_info3(mem_ctx,
653                                          validation_level, validation,
654                                          info3);
655         TALLOC_FREE(frame);
656         if (!NT_STATUS_IS_OK(status)) {
657                 return status;
658         }
659
660
661         return NT_STATUS_OK;
662 }
663
664 /**
665  * Logon domain user with an 'network' SAM logon
666  *
667  * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
668  **/
669
670
671 NTSTATUS rpccli_netlogon_network_logon(
672         struct netlogon_creds_cli_context *creds_ctx,
673         struct dcerpc_binding_handle *binding_handle,
674         TALLOC_CTX *mem_ctx,
675         uint32_t logon_parameters,
676         const char *username,
677         const char *domain,
678         const char *workstation,
679         const uint8_t chal[8],
680         DATA_BLOB lm_response,
681         DATA_BLOB nt_response,
682         uint8_t *authoritative,
683         uint32_t *flags,
684         struct netr_SamInfo3 **info3)
685 {
686         NTSTATUS status;
687         const char *workstation_name_slash;
688         union netr_LogonLevel *logon = NULL;
689         struct netr_NetworkInfo *network_info;
690         uint16_t validation_level = 0;
691         union netr_Validation *validation = NULL;
692         struct netr_ChallengeResponse lm;
693         struct netr_ChallengeResponse nt;
694
695         *info3 = NULL;
696
697         ZERO_STRUCT(lm);
698         ZERO_STRUCT(nt);
699
700         logon = talloc_zero(mem_ctx, union netr_LogonLevel);
701         if (!logon) {
702                 return NT_STATUS_NO_MEMORY;
703         }
704
705         network_info = talloc_zero(mem_ctx, struct netr_NetworkInfo);
706         if (!network_info) {
707                 return NT_STATUS_NO_MEMORY;
708         }
709
710         if (workstation[0] != '\\' && workstation[1] != '\\') {
711                 workstation_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", workstation);
712         } else {
713                 workstation_name_slash = workstation;
714         }
715
716         if (!workstation_name_slash) {
717                 DEBUG(0, ("talloc_asprintf failed!\n"));
718                 return NT_STATUS_NO_MEMORY;
719         }
720
721         /* Initialise input parameters */
722
723         lm.data = lm_response.data;
724         lm.length = lm_response.length;
725         nt.data = nt_response.data;
726         nt.length = nt_response.length;
727
728         network_info->identity_info.domain_name.string          = domain;
729         network_info->identity_info.parameter_control           = logon_parameters;
730         network_info->identity_info.logon_id_low                = 0xdead;
731         network_info->identity_info.logon_id_high               = 0xbeef;
732         network_info->identity_info.account_name.string         = username;
733         network_info->identity_info.workstation.string          = workstation_name_slash;
734
735         memcpy(network_info->challenge, chal, 8);
736         network_info->nt = nt;
737         network_info->lm = lm;
738
739         logon->network = network_info;
740
741         /* Marshall data and send request */
742
743         status = netlogon_creds_cli_LogonSamLogon(creds_ctx,
744                                                   binding_handle,
745                                                   NetlogonNetworkInformation,
746                                                   logon,
747                                                   mem_ctx,
748                                                   &validation_level,
749                                                   &validation,
750                                                   authoritative,
751                                                   flags);
752         if (!NT_STATUS_IS_OK(status)) {
753                 return status;
754         }
755
756         status = map_validation_to_info3(mem_ctx,
757                                          validation_level, validation,
758                                          info3);
759         if (!NT_STATUS_IS_OK(status)) {
760                 return status;
761         }
762
763         return NT_STATUS_OK;
764 }