s3:smbd: use req->xconn to send_trans2_replies()
[Samba.git] / source3 / rpc_client / cli_netlogon.c
blob7063351ef8a5600c4d5b17a1d798bba8c8c2f2f9
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(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) {
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 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,
94 TALLOC_CTX *mem_ctx,
95 struct netlogon_creds_cli_context **netlogon_creds)
97 TALLOC_CTX *frame = talloc_stackframe();
98 struct loadparm_context *lp_ctx;
99 NTSTATUS status;
101 status = rpccli_pre_open_netlogon_creds();
102 if (!NT_STATUS_IS_OK(status)) {
103 TALLOC_FREE(frame);
104 return status;
107 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
108 if (lp_ctx == NULL) {
109 TALLOC_FREE(frame);
110 return NT_STATUS_NO_MEMORY;
112 status = netlogon_creds_cli_context_global(lp_ctx,
113 msg_ctx,
114 client_account,
115 sec_chan_type,
116 server_computer,
117 server_netbios_domain,
118 mem_ctx, netlogon_creds);
119 TALLOC_FREE(frame);
120 if (!NT_STATUS_IS_OK(status)) {
121 return status;
124 return NT_STATUS_OK;
127 NTSTATUS rpccli_setup_netlogon_creds(struct cli_state *cli,
128 struct netlogon_creds_cli_context *netlogon_creds,
129 bool force_reauth,
130 struct samr_Password current_nt_hash,
131 const struct samr_Password *previous_nt_hash)
133 TALLOC_CTX *frame = talloc_stackframe();
134 struct rpc_pipe_client *netlogon_pipe = NULL;
135 struct netlogon_creds_CredentialState *creds = NULL;
136 NTSTATUS status;
138 status = netlogon_creds_cli_get(netlogon_creds,
139 frame, &creds);
140 if (NT_STATUS_IS_OK(status)) {
141 const char *action = "using";
143 if (force_reauth) {
144 action = "overwrite";
147 DEBUG(5,("%s: %s cached netlogon_creds cli[%s/%s] to %s\n",
148 __FUNCTION__, action,
149 creds->account_name, creds->computer_name,
150 smbXcli_conn_remote_name(cli->conn)));
151 if (!force_reauth) {
152 TALLOC_FREE(frame);
153 return NT_STATUS_OK;
155 TALLOC_FREE(creds);
158 status = cli_rpc_pipe_open_noauth(cli,
159 &ndr_table_netlogon,
160 &netlogon_pipe);
161 if (!NT_STATUS_IS_OK(status)) {
162 DEBUG(5,("%s: failed to open noauth netlogon connection to %s - %s\n",
163 __FUNCTION__,
164 smbXcli_conn_remote_name(cli->conn),
165 nt_errstr(status)));
166 TALLOC_FREE(frame);
167 return status;
169 talloc_steal(frame, netlogon_pipe);
171 status = netlogon_creds_cli_auth(netlogon_creds,
172 netlogon_pipe->binding_handle,
173 current_nt_hash,
174 previous_nt_hash);
175 if (!NT_STATUS_IS_OK(status)) {
176 TALLOC_FREE(frame);
177 return status;
180 status = netlogon_creds_cli_get(netlogon_creds,
181 frame, &creds);
182 if (!NT_STATUS_IS_OK(status)) {
183 TALLOC_FREE(frame);
184 return NT_STATUS_INTERNAL_ERROR;
187 DEBUG(5,("%s: using new netlogon_creds cli[%s/%s] to %s\n",
188 __FUNCTION__,
189 creds->account_name, creds->computer_name,
190 smbXcli_conn_remote_name(cli->conn)));
192 TALLOC_FREE(frame);
193 return NT_STATUS_OK;
196 static NTSTATUS map_validation_to_info3(TALLOC_CTX *mem_ctx,
197 uint16_t validation_level,
198 union netr_Validation *validation,
199 struct netr_SamInfo3 **info3_p)
201 struct netr_SamInfo3 *info3;
202 NTSTATUS status;
204 if (validation == NULL) {
205 return NT_STATUS_INVALID_PARAMETER;
208 switch (validation_level) {
209 case 3:
210 if (validation->sam3 == NULL) {
211 return NT_STATUS_INVALID_PARAMETER;
214 info3 = talloc_move(mem_ctx, &validation->sam3);
215 break;
216 case 6:
217 if (validation->sam6 == NULL) {
218 return NT_STATUS_INVALID_PARAMETER;
221 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
222 if (info3 == NULL) {
223 return NT_STATUS_NO_MEMORY;
225 status = copy_netr_SamBaseInfo(info3, &validation->sam6->base, &info3->base);
226 if (!NT_STATUS_IS_OK(status)) {
227 TALLOC_FREE(info3);
228 return status;
231 info3->sidcount = validation->sam6->sidcount;
232 info3->sids = talloc_move(info3, &validation->sam6->sids);
233 break;
234 default:
235 return NT_STATUS_BAD_VALIDATION_CLASS;
238 *info3_p = info3;
240 return NT_STATUS_OK;
243 /* Logon domain user */
245 NTSTATUS rpccli_netlogon_password_logon(struct netlogon_creds_cli_context *creds,
246 struct dcerpc_binding_handle *binding_handle,
247 TALLOC_CTX *mem_ctx,
248 uint32_t logon_parameters,
249 const char *domain,
250 const char *username,
251 const char *password,
252 const char *workstation,
253 enum netr_LogonInfoClass logon_type,
254 struct netr_SamInfo3 **info3)
256 TALLOC_CTX *frame = talloc_stackframe();
257 NTSTATUS status;
258 union netr_LogonLevel *logon;
259 uint16_t validation_level = 0;
260 union netr_Validation *validation = NULL;
261 uint8_t authoritative = 0;
262 uint32_t flags = 0;
263 char *workstation_slash = NULL;
265 logon = talloc_zero(frame, union netr_LogonLevel);
266 if (logon == NULL) {
267 TALLOC_FREE(frame);
268 return NT_STATUS_NO_MEMORY;
271 if (workstation == NULL) {
272 workstation = lp_netbios_name();
275 workstation_slash = talloc_asprintf(frame, "\\\\%s", workstation);
276 if (workstation_slash == NULL) {
277 TALLOC_FREE(frame);
278 return NT_STATUS_NO_MEMORY;
281 /* Initialise input parameters */
283 switch (logon_type) {
284 case NetlogonInteractiveInformation: {
286 struct netr_PasswordInfo *password_info;
288 struct samr_Password lmpassword;
289 struct samr_Password ntpassword;
291 password_info = talloc_zero(frame, struct netr_PasswordInfo);
292 if (password_info == NULL) {
293 TALLOC_FREE(frame);
294 return NT_STATUS_NO_MEMORY;
297 nt_lm_owf_gen(password, ntpassword.hash, lmpassword.hash);
299 password_info->identity_info.domain_name.string = domain;
300 password_info->identity_info.parameter_control = logon_parameters;
301 password_info->identity_info.logon_id_low = 0xdead;
302 password_info->identity_info.logon_id_high = 0xbeef;
303 password_info->identity_info.account_name.string = username;
304 password_info->identity_info.workstation.string = workstation_slash;
306 password_info->lmpassword = lmpassword;
307 password_info->ntpassword = ntpassword;
309 logon->password = password_info;
311 break;
313 case NetlogonNetworkInformation: {
314 struct netr_NetworkInfo *network_info;
315 uint8 chal[8];
316 unsigned char local_lm_response[24];
317 unsigned char local_nt_response[24];
318 struct netr_ChallengeResponse lm;
319 struct netr_ChallengeResponse nt;
321 ZERO_STRUCT(lm);
322 ZERO_STRUCT(nt);
324 network_info = talloc_zero(frame, struct netr_NetworkInfo);
325 if (network_info == NULL) {
326 TALLOC_FREE(frame);
327 return NT_STATUS_NO_MEMORY;
330 generate_random_buffer(chal, 8);
332 SMBencrypt(password, chal, local_lm_response);
333 SMBNTencrypt(password, chal, local_nt_response);
335 lm.length = 24;
336 lm.data = local_lm_response;
338 nt.length = 24;
339 nt.data = local_nt_response;
341 network_info->identity_info.domain_name.string = domain;
342 network_info->identity_info.parameter_control = logon_parameters;
343 network_info->identity_info.logon_id_low = 0xdead;
344 network_info->identity_info.logon_id_high = 0xbeef;
345 network_info->identity_info.account_name.string = username;
346 network_info->identity_info.workstation.string = workstation_slash;
348 memcpy(network_info->challenge, chal, 8);
349 network_info->nt = nt;
350 network_info->lm = lm;
352 logon->network = network_info;
354 break;
356 default:
357 DEBUG(0, ("switch value %d not supported\n",
358 logon_type));
359 TALLOC_FREE(frame);
360 return NT_STATUS_INVALID_INFO_CLASS;
363 status = netlogon_creds_cli_LogonSamLogon(creds,
364 binding_handle,
365 logon_type,
366 logon,
367 frame,
368 &validation_level,
369 &validation,
370 &authoritative,
371 &flags);
372 if (!NT_STATUS_IS_OK(status)) {
373 TALLOC_FREE(frame);
374 return status;
377 status = map_validation_to_info3(mem_ctx,
378 validation_level, validation,
379 info3);
380 TALLOC_FREE(frame);
381 if (!NT_STATUS_IS_OK(status)) {
382 return status;
386 return NT_STATUS_OK;
390 * Logon domain user with an 'network' SAM logon
392 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
396 NTSTATUS rpccli_netlogon_network_logon(struct netlogon_creds_cli_context *creds,
397 struct dcerpc_binding_handle *binding_handle,
398 TALLOC_CTX *mem_ctx,
399 uint32_t logon_parameters,
400 const char *username,
401 const char *domain,
402 const char *workstation,
403 const uint8 chal[8],
404 DATA_BLOB lm_response,
405 DATA_BLOB nt_response,
406 uint8_t *authoritative,
407 uint32_t *flags,
408 struct netr_SamInfo3 **info3)
410 NTSTATUS status;
411 const char *workstation_name_slash;
412 union netr_LogonLevel *logon = NULL;
413 struct netr_NetworkInfo *network_info;
414 uint16_t validation_level = 0;
415 union netr_Validation *validation = NULL;
416 uint8_t _authoritative = 0;
417 uint32_t _flags = 0;
418 struct netr_ChallengeResponse lm;
419 struct netr_ChallengeResponse nt;
421 *info3 = NULL;
423 if (authoritative == NULL) {
424 authoritative = &_authoritative;
426 if (flags == NULL) {
427 flags = &_flags;
430 ZERO_STRUCT(lm);
431 ZERO_STRUCT(nt);
433 logon = talloc_zero(mem_ctx, union netr_LogonLevel);
434 if (!logon) {
435 return NT_STATUS_NO_MEMORY;
438 network_info = talloc_zero(mem_ctx, struct netr_NetworkInfo);
439 if (!network_info) {
440 return NT_STATUS_NO_MEMORY;
443 if (workstation[0] != '\\' && workstation[1] != '\\') {
444 workstation_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", workstation);
445 } else {
446 workstation_name_slash = workstation;
449 if (!workstation_name_slash) {
450 DEBUG(0, ("talloc_asprintf failed!\n"));
451 return NT_STATUS_NO_MEMORY;
454 /* Initialise input parameters */
456 lm.data = lm_response.data;
457 lm.length = lm_response.length;
458 nt.data = nt_response.data;
459 nt.length = nt_response.length;
461 network_info->identity_info.domain_name.string = domain;
462 network_info->identity_info.parameter_control = logon_parameters;
463 network_info->identity_info.logon_id_low = 0xdead;
464 network_info->identity_info.logon_id_high = 0xbeef;
465 network_info->identity_info.account_name.string = username;
466 network_info->identity_info.workstation.string = workstation_name_slash;
468 memcpy(network_info->challenge, chal, 8);
469 network_info->nt = nt;
470 network_info->lm = lm;
472 logon->network = network_info;
474 /* Marshall data and send request */
476 status = netlogon_creds_cli_LogonSamLogon(creds,
477 binding_handle,
478 NetlogonNetworkInformation,
479 logon,
480 mem_ctx,
481 &validation_level,
482 &validation,
483 authoritative,
484 flags);
485 if (!NT_STATUS_IS_OK(status)) {
486 return status;
489 status = map_validation_to_info3(mem_ctx,
490 validation_level, validation,
491 info3);
492 if (!NT_STATUS_IS_OK(status)) {
493 return status;
496 return NT_STATUS_OK;