cli_netlogon: Return flags from rpccli_setup_netlogon_creds_locked
[Samba.git] / source3 / rpc_client / cli_netlogon.c
blob6734442cd0d8aec786265c89304fb7ca2958fc31
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.
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.
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.
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/>.
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"
43 NTSTATUS rpccli_pre_open_netlogon_creds(void)
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;
52 if (already_open) {
53 return NT_STATUS_OK;
56 frame = talloc_stackframe();
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;
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;
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;
79 status = netlogon_creds_cli_set_global_db(&global_db);
80 TALLOC_FREE(frame);
81 if (!NT_STATUS_IS_OK(status)) {
82 return status;
85 already_open = true;
86 return NT_STATUS_OK;
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)
99 TALLOC_CTX *frame = talloc_stackframe();
100 struct loadparm_context *lp_ctx;
101 NTSTATUS status;
103 status = rpccli_pre_open_netlogon_creds();
104 if (!NT_STATUS_IS_OK(status)) {
105 TALLOC_FREE(frame);
106 return status;
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;
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;
127 return NT_STATUS_OK;
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)
137 enum netr_SchannelType sec_chan_type;
138 const char *server_netbios_domain;
139 const char *server_dns_domain;
140 const char *client_account;
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);
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);
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)
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;
172 status = netlogon_creds_cli_get(creds_ctx, frame, &creds);
173 if (NT_STATUS_IS_OK(status)) {
174 const char *action = "using";
176 if (force_reauth) {
177 action = "overwrite";
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;
187 TALLOC_FREE(creds);
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;
195 num_nt_hashes = 1;
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;
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;
215 talloc_steal(frame, netlogon_pipe);
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;
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;
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)));
238 done:
239 if (negotiate_flags != NULL) {
240 *negotiate_flags = creds->negotiate_flags;
243 TALLOC_FREE(frame);
244 return NT_STATUS_OK;
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)
254 TALLOC_CTX *frame = talloc_stackframe();
255 struct netlogon_creds_cli_lck *lck;
256 NTSTATUS status;
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;
268 status = rpccli_setup_netlogon_creds_locked(
269 cli, transport, creds_ctx, force_reauth, cli_creds, NULL);
271 TALLOC_FREE(frame);
273 return status;
276 static NTSTATUS map_validation_to_info3(TALLOC_CTX *mem_ctx,
277 uint16_t validation_level,
278 union netr_Validation *validation,
279 struct netr_SamInfo3 **info3_p)
281 struct netr_SamInfo3 *info3;
282 NTSTATUS status;
284 if (validation == NULL) {
285 return NT_STATUS_INVALID_PARAMETER;
288 switch (validation_level) {
289 case 3:
290 if (validation->sam3 == NULL) {
291 return NT_STATUS_INVALID_PARAMETER;
294 info3 = talloc_move(mem_ctx, &validation->sam3);
295 break;
296 case 6:
297 if (validation->sam6 == NULL) {
298 return NT_STATUS_INVALID_PARAMETER;
301 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
302 if (info3 == NULL) {
303 return NT_STATUS_NO_MEMORY;
305 status = copy_netr_SamBaseInfo(info3, &validation->sam6->base, &info3->base);
306 if (!NT_STATUS_IS_OK(status)) {
307 TALLOC_FREE(info3);
308 return status;
311 info3->sidcount = validation->sam6->sidcount;
312 info3->sids = talloc_move(info3, &validation->sam6->sids);
313 break;
314 default:
315 return NT_STATUS_BAD_VALIDATION_CLASS;
318 *info3_p = info3;
320 return NT_STATUS_OK;
323 /* Logon domain user */
325 NTSTATUS rpccli_netlogon_password_logon(
326 struct netlogon_creds_cli_context *creds_ctx,
327 struct dcerpc_binding_handle *binding_handle,
328 TALLOC_CTX *mem_ctx,
329 uint32_t logon_parameters,
330 const char *domain,
331 const char *username,
332 const char *password,
333 const char *workstation,
334 enum netr_LogonInfoClass logon_type,
335 uint8_t *authoritative,
336 uint32_t *flags,
337 struct netr_SamInfo3 **info3)
339 TALLOC_CTX *frame = talloc_stackframe();
340 NTSTATUS status;
341 union netr_LogonLevel *logon;
342 uint16_t validation_level = 0;
343 union netr_Validation *validation = NULL;
344 char *workstation_slash = NULL;
346 logon = talloc_zero(frame, union netr_LogonLevel);
347 if (logon == NULL) {
348 TALLOC_FREE(frame);
349 return NT_STATUS_NO_MEMORY;
352 if (workstation == NULL) {
353 workstation = lp_netbios_name();
356 workstation_slash = talloc_asprintf(frame, "\\\\%s", workstation);
357 if (workstation_slash == NULL) {
358 TALLOC_FREE(frame);
359 return NT_STATUS_NO_MEMORY;
362 /* Initialise input parameters */
364 switch (logon_type) {
365 case NetlogonInteractiveInformation: {
367 struct netr_PasswordInfo *password_info;
369 struct samr_Password lmpassword;
370 struct samr_Password ntpassword;
372 password_info = talloc_zero(frame, struct netr_PasswordInfo);
373 if (password_info == NULL) {
374 TALLOC_FREE(frame);
375 return NT_STATUS_NO_MEMORY;
378 nt_lm_owf_gen(password, ntpassword.hash, lmpassword.hash);
380 password_info->identity_info.domain_name.string = domain;
381 password_info->identity_info.parameter_control = logon_parameters;
382 password_info->identity_info.logon_id_low = 0xdead;
383 password_info->identity_info.logon_id_high = 0xbeef;
384 password_info->identity_info.account_name.string = username;
385 password_info->identity_info.workstation.string = workstation_slash;
387 password_info->lmpassword = lmpassword;
388 password_info->ntpassword = ntpassword;
390 logon->password = password_info;
392 break;
394 case NetlogonNetworkInformation: {
395 struct netr_NetworkInfo *network_info;
396 uint8_t chal[8];
397 unsigned char local_lm_response[24];
398 unsigned char local_nt_response[24];
399 struct netr_ChallengeResponse lm;
400 struct netr_ChallengeResponse nt;
402 ZERO_STRUCT(lm);
403 ZERO_STRUCT(nt);
405 network_info = talloc_zero(frame, struct netr_NetworkInfo);
406 if (network_info == NULL) {
407 TALLOC_FREE(frame);
408 return NT_STATUS_NO_MEMORY;
411 generate_random_buffer(chal, 8);
413 SMBencrypt(password, chal, local_lm_response);
414 SMBNTencrypt(password, chal, local_nt_response);
416 lm.length = 24;
417 lm.data = local_lm_response;
419 nt.length = 24;
420 nt.data = local_nt_response;
422 network_info->identity_info.domain_name.string = domain;
423 network_info->identity_info.parameter_control = logon_parameters;
424 network_info->identity_info.logon_id_low = 0xdead;
425 network_info->identity_info.logon_id_high = 0xbeef;
426 network_info->identity_info.account_name.string = username;
427 network_info->identity_info.workstation.string = workstation_slash;
429 memcpy(network_info->challenge, chal, 8);
430 network_info->nt = nt;
431 network_info->lm = lm;
433 logon->network = network_info;
435 break;
437 default:
438 DEBUG(0, ("switch value %d not supported\n",
439 logon_type));
440 TALLOC_FREE(frame);
441 return NT_STATUS_INVALID_INFO_CLASS;
444 status = netlogon_creds_cli_LogonSamLogon(creds_ctx,
445 binding_handle,
446 logon_type,
447 logon,
448 frame,
449 &validation_level,
450 &validation,
451 authoritative,
452 flags);
453 if (!NT_STATUS_IS_OK(status)) {
454 TALLOC_FREE(frame);
455 return status;
458 status = map_validation_to_info3(mem_ctx,
459 validation_level, validation,
460 info3);
461 TALLOC_FREE(frame);
462 if (!NT_STATUS_IS_OK(status)) {
463 return status;
467 return NT_STATUS_OK;
471 * Logon domain user with an 'network' SAM logon
473 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
477 NTSTATUS rpccli_netlogon_network_logon(
478 struct netlogon_creds_cli_context *creds_ctx,
479 struct dcerpc_binding_handle *binding_handle,
480 TALLOC_CTX *mem_ctx,
481 uint32_t logon_parameters,
482 const char *username,
483 const char *domain,
484 const char *workstation,
485 const uint8_t chal[8],
486 DATA_BLOB lm_response,
487 DATA_BLOB nt_response,
488 uint8_t *authoritative,
489 uint32_t *flags,
490 struct netr_SamInfo3 **info3)
492 NTSTATUS status;
493 const char *workstation_name_slash;
494 union netr_LogonLevel *logon = NULL;
495 struct netr_NetworkInfo *network_info;
496 uint16_t validation_level = 0;
497 union netr_Validation *validation = NULL;
498 struct netr_ChallengeResponse lm;
499 struct netr_ChallengeResponse nt;
501 *info3 = NULL;
503 ZERO_STRUCT(lm);
504 ZERO_STRUCT(nt);
506 logon = talloc_zero(mem_ctx, union netr_LogonLevel);
507 if (!logon) {
508 return NT_STATUS_NO_MEMORY;
511 network_info = talloc_zero(mem_ctx, struct netr_NetworkInfo);
512 if (!network_info) {
513 return NT_STATUS_NO_MEMORY;
516 if (workstation[0] != '\\' && workstation[1] != '\\') {
517 workstation_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", workstation);
518 } else {
519 workstation_name_slash = workstation;
522 if (!workstation_name_slash) {
523 DEBUG(0, ("talloc_asprintf failed!\n"));
524 return NT_STATUS_NO_MEMORY;
527 /* Initialise input parameters */
529 lm.data = lm_response.data;
530 lm.length = lm_response.length;
531 nt.data = nt_response.data;
532 nt.length = nt_response.length;
534 network_info->identity_info.domain_name.string = domain;
535 network_info->identity_info.parameter_control = logon_parameters;
536 network_info->identity_info.logon_id_low = 0xdead;
537 network_info->identity_info.logon_id_high = 0xbeef;
538 network_info->identity_info.account_name.string = username;
539 network_info->identity_info.workstation.string = workstation_name_slash;
541 memcpy(network_info->challenge, chal, 8);
542 network_info->nt = nt;
543 network_info->lm = lm;
545 logon->network = network_info;
547 /* Marshall data and send request */
549 status = netlogon_creds_cli_LogonSamLogon(creds_ctx,
550 binding_handle,
551 NetlogonNetworkInformation,
552 logon,
553 mem_ctx,
554 &validation_level,
555 &validation,
556 authoritative,
557 flags);
558 if (!NT_STATUS_IS_OK(status)) {
559 return status;
562 status = map_validation_to_info3(mem_ctx,
563 validation_level, validation,
564 info3);
565 if (!NT_STATUS_IS_OK(status)) {
566 return status;
569 return NT_STATUS_OK;