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/>.
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"
43 NTSTATUS rpccli_pre_open_netlogon_creds(void)
45 static bool already_open = false;
47 struct loadparm_context *lp_ctx;
49 struct db_context *global_db;
56 frame = talloc_stackframe();
58 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
61 return NT_STATUS_NO_MEMORY;
64 fname = lpcfg_private_db_path(frame, lp_ctx, "netlogon_creds_cli");
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) {
76 return NT_STATUS_NO_MEMORY;
79 status = netlogon_creds_cli_set_global_db(&global_db);
81 if (!NT_STATUS_IS_OK(status)) {
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,
97 struct netlogon_creds_cli_context **netlogon_creds)
99 TALLOC_CTX *frame = talloc_stackframe();
100 struct loadparm_context *lp_ctx;
103 status = rpccli_pre_open_netlogon_creds();
104 if (!NT_STATUS_IS_OK(status)) {
109 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
110 if (lp_ctx == NULL) {
112 return NT_STATUS_NO_MEMORY;
114 status = netlogon_creds_cli_context_global(lp_ctx,
119 server_netbios_domain,
121 mem_ctx, netlogon_creds);
123 if (!NT_STATUS_IS_OK(status)) {
130 NTSTATUS rpccli_create_netlogon_creds_ctx(
131 struct cli_credentials *creds,
132 const char *server_computer,
133 struct messaging_context *msg_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,
156 NTSTATUS rpccli_setup_netlogon_creds(
157 struct cli_state *cli,
158 enum dcerpc_transport_t transport,
159 struct netlogon_creds_cli_context *creds_ctx,
161 struct cli_credentials *cli_creds)
163 TALLOC_CTX *frame = talloc_stackframe();
164 struct rpc_pipe_client *netlogon_pipe = NULL;
165 struct netlogon_creds_CredentialState *creds = NULL;
166 uint8_t num_nt_hashes = 0;
167 const struct samr_Password *nt_hashes[2] = { NULL, NULL };
168 uint8_t idx_nt_hashes = 0;
171 status = netlogon_creds_cli_get(creds_ctx, frame, &creds);
172 if (NT_STATUS_IS_OK(status)) {
173 const char *action = "using";
176 action = "overwrite";
179 DEBUG(5,("%s: %s cached netlogon_creds cli[%s/%s] to %s\n",
180 __FUNCTION__, action,
181 creds->account_name, creds->computer_name,
182 smbXcli_conn_remote_name(cli->conn)));
190 nt_hashes[0] = cli_credentials_get_nt_hash(cli_creds, talloc_tos());
191 if (nt_hashes[0] == NULL) {
193 return NT_STATUS_NO_MEMORY;
197 nt_hashes[1] = cli_credentials_get_old_nt_hash(cli_creds,
199 if (nt_hashes[1] != NULL) {
203 status = cli_rpc_pipe_open_noauth_transport(cli,
207 if (!NT_STATUS_IS_OK(status)) {
208 DEBUG(5,("%s: failed to open noauth netlogon connection to %s - %s\n",
210 smbXcli_conn_remote_name(cli->conn),
215 talloc_steal(frame, netlogon_pipe);
217 status = netlogon_creds_cli_auth(creds_ctx,
218 netlogon_pipe->binding_handle,
222 if (!NT_STATUS_IS_OK(status)) {
227 status = netlogon_creds_cli_get(creds_ctx, frame, &creds);
228 if (!NT_STATUS_IS_OK(status)) {
230 return NT_STATUS_INTERNAL_ERROR;
233 DEBUG(5,("%s: using new netlogon_creds cli[%s/%s] to %s\n",
235 creds->account_name, creds->computer_name,
236 smbXcli_conn_remote_name(cli->conn)));
242 static NTSTATUS map_validation_to_info3(TALLOC_CTX *mem_ctx,
243 uint16_t validation_level,
244 union netr_Validation *validation,
245 struct netr_SamInfo3 **info3_p)
247 struct netr_SamInfo3 *info3;
250 if (validation == NULL) {
251 return NT_STATUS_INVALID_PARAMETER;
254 switch (validation_level) {
256 if (validation->sam3 == NULL) {
257 return NT_STATUS_INVALID_PARAMETER;
260 info3 = talloc_move(mem_ctx, &validation->sam3);
263 if (validation->sam6 == NULL) {
264 return NT_STATUS_INVALID_PARAMETER;
267 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
269 return NT_STATUS_NO_MEMORY;
271 status = copy_netr_SamBaseInfo(info3, &validation->sam6->base, &info3->base);
272 if (!NT_STATUS_IS_OK(status)) {
277 info3->sidcount = validation->sam6->sidcount;
278 info3->sids = talloc_move(info3, &validation->sam6->sids);
281 return NT_STATUS_BAD_VALIDATION_CLASS;
289 /* Logon domain user */
291 NTSTATUS rpccli_netlogon_password_logon(
292 struct netlogon_creds_cli_context *creds_ctx,
293 struct dcerpc_binding_handle *binding_handle,
295 uint32_t logon_parameters,
297 const char *username,
298 const char *password,
299 const char *workstation,
300 enum netr_LogonInfoClass logon_type,
301 uint8_t *authoritative,
303 struct netr_SamInfo3 **info3)
305 TALLOC_CTX *frame = talloc_stackframe();
307 union netr_LogonLevel *logon;
308 uint16_t validation_level = 0;
309 union netr_Validation *validation = NULL;
310 char *workstation_slash = NULL;
312 logon = talloc_zero(frame, union netr_LogonLevel);
315 return NT_STATUS_NO_MEMORY;
318 if (workstation == NULL) {
319 workstation = lp_netbios_name();
322 workstation_slash = talloc_asprintf(frame, "\\\\%s", workstation);
323 if (workstation_slash == NULL) {
325 return NT_STATUS_NO_MEMORY;
328 /* Initialise input parameters */
330 switch (logon_type) {
331 case NetlogonInteractiveInformation: {
333 struct netr_PasswordInfo *password_info;
335 struct samr_Password lmpassword;
336 struct samr_Password ntpassword;
338 password_info = talloc_zero(frame, struct netr_PasswordInfo);
339 if (password_info == NULL) {
341 return NT_STATUS_NO_MEMORY;
344 nt_lm_owf_gen(password, ntpassword.hash, lmpassword.hash);
346 password_info->identity_info.domain_name.string = domain;
347 password_info->identity_info.parameter_control = logon_parameters;
348 password_info->identity_info.logon_id_low = 0xdead;
349 password_info->identity_info.logon_id_high = 0xbeef;
350 password_info->identity_info.account_name.string = username;
351 password_info->identity_info.workstation.string = workstation_slash;
353 password_info->lmpassword = lmpassword;
354 password_info->ntpassword = ntpassword;
356 logon->password = password_info;
360 case NetlogonNetworkInformation: {
361 struct netr_NetworkInfo *network_info;
363 unsigned char local_lm_response[24];
364 unsigned char local_nt_response[24];
365 struct netr_ChallengeResponse lm;
366 struct netr_ChallengeResponse nt;
371 network_info = talloc_zero(frame, struct netr_NetworkInfo);
372 if (network_info == NULL) {
374 return NT_STATUS_NO_MEMORY;
377 generate_random_buffer(chal, 8);
379 SMBencrypt(password, chal, local_lm_response);
380 SMBNTencrypt(password, chal, local_nt_response);
383 lm.data = local_lm_response;
386 nt.data = local_nt_response;
388 network_info->identity_info.domain_name.string = domain;
389 network_info->identity_info.parameter_control = logon_parameters;
390 network_info->identity_info.logon_id_low = 0xdead;
391 network_info->identity_info.logon_id_high = 0xbeef;
392 network_info->identity_info.account_name.string = username;
393 network_info->identity_info.workstation.string = workstation_slash;
395 memcpy(network_info->challenge, chal, 8);
396 network_info->nt = nt;
397 network_info->lm = lm;
399 logon->network = network_info;
404 DEBUG(0, ("switch value %d not supported\n",
407 return NT_STATUS_INVALID_INFO_CLASS;
410 status = netlogon_creds_cli_LogonSamLogon(creds_ctx,
419 if (!NT_STATUS_IS_OK(status)) {
424 status = map_validation_to_info3(mem_ctx,
425 validation_level, validation,
428 if (!NT_STATUS_IS_OK(status)) {
437 * Logon domain user with an 'network' SAM logon
439 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
443 NTSTATUS rpccli_netlogon_network_logon(
444 struct netlogon_creds_cli_context *creds_ctx,
445 struct dcerpc_binding_handle *binding_handle,
447 uint32_t logon_parameters,
448 const char *username,
450 const char *workstation,
451 const uint8_t chal[8],
452 DATA_BLOB lm_response,
453 DATA_BLOB nt_response,
454 uint8_t *authoritative,
456 struct netr_SamInfo3 **info3)
459 const char *workstation_name_slash;
460 union netr_LogonLevel *logon = NULL;
461 struct netr_NetworkInfo *network_info;
462 uint16_t validation_level = 0;
463 union netr_Validation *validation = NULL;
464 struct netr_ChallengeResponse lm;
465 struct netr_ChallengeResponse nt;
472 logon = talloc_zero(mem_ctx, union netr_LogonLevel);
474 return NT_STATUS_NO_MEMORY;
477 network_info = talloc_zero(mem_ctx, struct netr_NetworkInfo);
479 return NT_STATUS_NO_MEMORY;
482 if (workstation[0] != '\\' && workstation[1] != '\\') {
483 workstation_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", workstation);
485 workstation_name_slash = workstation;
488 if (!workstation_name_slash) {
489 DEBUG(0, ("talloc_asprintf failed!\n"));
490 return NT_STATUS_NO_MEMORY;
493 /* Initialise input parameters */
495 lm.data = lm_response.data;
496 lm.length = lm_response.length;
497 nt.data = nt_response.data;
498 nt.length = nt_response.length;
500 network_info->identity_info.domain_name.string = domain;
501 network_info->identity_info.parameter_control = logon_parameters;
502 network_info->identity_info.logon_id_low = 0xdead;
503 network_info->identity_info.logon_id_high = 0xbeef;
504 network_info->identity_info.account_name.string = username;
505 network_info->identity_info.workstation.string = workstation_name_slash;
507 memcpy(network_info->challenge, chal, 8);
508 network_info->nt = nt;
509 network_info->lm = lm;
511 logon->network = network_info;
513 /* Marshall data and send request */
515 status = netlogon_creds_cli_LogonSamLogon(creds_ctx,
517 NetlogonNetworkInformation,
524 if (!NT_STATUS_IS_OK(status)) {
528 status = map_validation_to_info3(mem_ctx,
529 validation_level, validation,
531 if (!NT_STATUS_IS_OK(status)) {