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(talloc_autofree_context(), 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 NTSTATUS rpccli_create_netlogon_creds(const char *server_computer,
90 const char *server_netbios_domain,
91 const char *client_account,
92 enum netr_SchannelType sec_chan_type,
93 struct messaging_context *msg_ctx,
95 struct netlogon_creds_cli_context **netlogon_creds)
97 TALLOC_CTX *frame = talloc_stackframe();
98 struct loadparm_context *lp_ctx;
101 status = rpccli_pre_open_netlogon_creds();
102 if (!NT_STATUS_IS_OK(status)) {
107 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
108 if (lp_ctx == NULL) {
110 return NT_STATUS_NO_MEMORY;
112 status = netlogon_creds_cli_context_global(lp_ctx,
117 server_netbios_domain,
118 mem_ctx, netlogon_creds);
120 if (!NT_STATUS_IS_OK(status)) {
127 NTSTATUS rpccli_create_netlogon_creds_with_creds(struct cli_credentials *creds,
128 const char *server_computer,
129 struct messaging_context *msg_ctx,
131 struct netlogon_creds_cli_context **netlogon_creds)
133 enum netr_SchannelType sec_chan_type;
134 const char *server_netbios_domain;
135 const char *client_account;
137 sec_chan_type = cli_credentials_get_secure_channel_type(creds);
138 if (sec_chan_type == SEC_CHAN_NULL) {
139 return NT_STATUS_INVALID_PARAMETER_MIX;
142 client_account = cli_credentials_get_username(creds);
143 server_netbios_domain = cli_credentials_get_domain(creds);
145 return rpccli_create_netlogon_creds(server_computer,
146 server_netbios_domain,
153 NTSTATUS rpccli_setup_netlogon_creds(struct cli_state *cli,
154 enum dcerpc_transport_t transport,
155 struct netlogon_creds_cli_context *netlogon_creds,
157 struct samr_Password current_nt_hash,
158 const struct samr_Password *previous_nt_hash)
160 TALLOC_CTX *frame = talloc_stackframe();
161 struct rpc_pipe_client *netlogon_pipe = NULL;
162 struct netlogon_creds_CredentialState *creds = NULL;
165 status = netlogon_creds_cli_get(netlogon_creds,
167 if (NT_STATUS_IS_OK(status)) {
168 const char *action = "using";
171 action = "overwrite";
174 DEBUG(5,("%s: %s cached netlogon_creds cli[%s/%s] to %s\n",
175 __FUNCTION__, action,
176 creds->account_name, creds->computer_name,
177 smbXcli_conn_remote_name(cli->conn)));
185 status = cli_rpc_pipe_open_noauth_transport(cli,
189 if (!NT_STATUS_IS_OK(status)) {
190 DEBUG(5,("%s: failed to open noauth netlogon connection to %s - %s\n",
192 smbXcli_conn_remote_name(cli->conn),
197 talloc_steal(frame, netlogon_pipe);
199 status = netlogon_creds_cli_auth(netlogon_creds,
200 netlogon_pipe->binding_handle,
203 if (!NT_STATUS_IS_OK(status)) {
208 status = netlogon_creds_cli_get(netlogon_creds,
210 if (!NT_STATUS_IS_OK(status)) {
212 return NT_STATUS_INTERNAL_ERROR;
215 DEBUG(5,("%s: using new netlogon_creds cli[%s/%s] to %s\n",
217 creds->account_name, creds->computer_name,
218 smbXcli_conn_remote_name(cli->conn)));
224 NTSTATUS rpccli_setup_netlogon_creds_with_creds(struct cli_state *cli,
225 enum dcerpc_transport_t transport,
226 struct netlogon_creds_cli_context *netlogon_creds,
228 struct cli_credentials *creds)
230 struct samr_Password *current_nt_hash = NULL;
231 struct samr_Password *previous_nt_hash = NULL;
234 current_nt_hash = cli_credentials_get_nt_hash(creds, talloc_tos());
235 if (current_nt_hash == NULL) {
236 return NT_STATUS_NO_MEMORY;
239 status = rpccli_setup_netlogon_creds(cli, transport,
244 TALLOC_FREE(current_nt_hash);
245 if (!NT_STATUS_IS_OK(status)) {
252 static NTSTATUS map_validation_to_info3(TALLOC_CTX *mem_ctx,
253 uint16_t validation_level,
254 union netr_Validation *validation,
255 struct netr_SamInfo3 **info3_p)
257 struct netr_SamInfo3 *info3;
260 if (validation == NULL) {
261 return NT_STATUS_INVALID_PARAMETER;
264 switch (validation_level) {
266 if (validation->sam3 == NULL) {
267 return NT_STATUS_INVALID_PARAMETER;
270 info3 = talloc_move(mem_ctx, &validation->sam3);
273 if (validation->sam6 == NULL) {
274 return NT_STATUS_INVALID_PARAMETER;
277 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
279 return NT_STATUS_NO_MEMORY;
281 status = copy_netr_SamBaseInfo(info3, &validation->sam6->base, &info3->base);
282 if (!NT_STATUS_IS_OK(status)) {
287 info3->sidcount = validation->sam6->sidcount;
288 info3->sids = talloc_move(info3, &validation->sam6->sids);
291 return NT_STATUS_BAD_VALIDATION_CLASS;
299 /* Logon domain user */
301 NTSTATUS rpccli_netlogon_password_logon(struct netlogon_creds_cli_context *creds,
302 struct dcerpc_binding_handle *binding_handle,
304 uint32_t logon_parameters,
306 const char *username,
307 const char *password,
308 const char *workstation,
309 enum netr_LogonInfoClass logon_type,
310 struct netr_SamInfo3 **info3)
312 TALLOC_CTX *frame = talloc_stackframe();
314 union netr_LogonLevel *logon;
315 uint16_t validation_level = 0;
316 union netr_Validation *validation = NULL;
317 uint8_t authoritative = 0;
319 char *workstation_slash = NULL;
321 logon = talloc_zero(frame, union netr_LogonLevel);
324 return NT_STATUS_NO_MEMORY;
327 if (workstation == NULL) {
328 workstation = lp_netbios_name();
331 workstation_slash = talloc_asprintf(frame, "\\\\%s", workstation);
332 if (workstation_slash == NULL) {
334 return NT_STATUS_NO_MEMORY;
337 /* Initialise input parameters */
339 switch (logon_type) {
340 case NetlogonInteractiveInformation: {
342 struct netr_PasswordInfo *password_info;
344 struct samr_Password lmpassword;
345 struct samr_Password ntpassword;
347 password_info = talloc_zero(frame, struct netr_PasswordInfo);
348 if (password_info == NULL) {
350 return NT_STATUS_NO_MEMORY;
353 nt_lm_owf_gen(password, ntpassword.hash, lmpassword.hash);
355 password_info->identity_info.domain_name.string = domain;
356 password_info->identity_info.parameter_control = logon_parameters;
357 password_info->identity_info.logon_id_low = 0xdead;
358 password_info->identity_info.logon_id_high = 0xbeef;
359 password_info->identity_info.account_name.string = username;
360 password_info->identity_info.workstation.string = workstation_slash;
362 password_info->lmpassword = lmpassword;
363 password_info->ntpassword = ntpassword;
365 logon->password = password_info;
369 case NetlogonNetworkInformation: {
370 struct netr_NetworkInfo *network_info;
372 unsigned char local_lm_response[24];
373 unsigned char local_nt_response[24];
374 struct netr_ChallengeResponse lm;
375 struct netr_ChallengeResponse nt;
380 network_info = talloc_zero(frame, struct netr_NetworkInfo);
381 if (network_info == NULL) {
383 return NT_STATUS_NO_MEMORY;
386 generate_random_buffer(chal, 8);
388 SMBencrypt(password, chal, local_lm_response);
389 SMBNTencrypt(password, chal, local_nt_response);
392 lm.data = local_lm_response;
395 nt.data = local_nt_response;
397 network_info->identity_info.domain_name.string = domain;
398 network_info->identity_info.parameter_control = logon_parameters;
399 network_info->identity_info.logon_id_low = 0xdead;
400 network_info->identity_info.logon_id_high = 0xbeef;
401 network_info->identity_info.account_name.string = username;
402 network_info->identity_info.workstation.string = workstation_slash;
404 memcpy(network_info->challenge, chal, 8);
405 network_info->nt = nt;
406 network_info->lm = lm;
408 logon->network = network_info;
413 DEBUG(0, ("switch value %d not supported\n",
416 return NT_STATUS_INVALID_INFO_CLASS;
419 status = netlogon_creds_cli_LogonSamLogon(creds,
428 if (!NT_STATUS_IS_OK(status)) {
433 status = map_validation_to_info3(mem_ctx,
434 validation_level, validation,
437 if (!NT_STATUS_IS_OK(status)) {
446 * Logon domain user with an 'network' SAM logon
448 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
452 NTSTATUS rpccli_netlogon_network_logon(struct netlogon_creds_cli_context *creds,
453 struct dcerpc_binding_handle *binding_handle,
455 uint32_t logon_parameters,
456 const char *username,
458 const char *workstation,
460 DATA_BLOB lm_response,
461 DATA_BLOB nt_response,
462 uint8_t *authoritative,
464 struct netr_SamInfo3 **info3)
467 const char *workstation_name_slash;
468 union netr_LogonLevel *logon = NULL;
469 struct netr_NetworkInfo *network_info;
470 uint16_t validation_level = 0;
471 union netr_Validation *validation = NULL;
472 uint8_t _authoritative = 0;
474 struct netr_ChallengeResponse lm;
475 struct netr_ChallengeResponse nt;
479 if (authoritative == NULL) {
480 authoritative = &_authoritative;
489 logon = talloc_zero(mem_ctx, union netr_LogonLevel);
491 return NT_STATUS_NO_MEMORY;
494 network_info = talloc_zero(mem_ctx, struct netr_NetworkInfo);
496 return NT_STATUS_NO_MEMORY;
499 if (workstation[0] != '\\' && workstation[1] != '\\') {
500 workstation_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", workstation);
502 workstation_name_slash = workstation;
505 if (!workstation_name_slash) {
506 DEBUG(0, ("talloc_asprintf failed!\n"));
507 return NT_STATUS_NO_MEMORY;
510 /* Initialise input parameters */
512 lm.data = lm_response.data;
513 lm.length = lm_response.length;
514 nt.data = nt_response.data;
515 nt.length = nt_response.length;
517 network_info->identity_info.domain_name.string = domain;
518 network_info->identity_info.parameter_control = logon_parameters;
519 network_info->identity_info.logon_id_low = 0xdead;
520 network_info->identity_info.logon_id_high = 0xbeef;
521 network_info->identity_info.account_name.string = username;
522 network_info->identity_info.workstation.string = workstation_name_slash;
524 memcpy(network_info->challenge, chal, 8);
525 network_info->nt = nt;
526 network_info->lm = lm;
528 logon->network = network_info;
530 /* Marshall data and send request */
532 status = netlogon_creds_cli_LogonSamLogon(creds,
534 NetlogonNetworkInformation,
541 if (!NT_STATUS_IS_OK(status)) {
545 status = map_validation_to_info3(mem_ctx,
546 validation_level, validation,
548 if (!NT_STATUS_IS_OK(status)) {