2 Unix SMB/CIFS implementation.
4 dcerpc torture tests, designed to walk Samba3 code paths
6 Copyright (C) Volker Lendecke 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "torture/torture.h"
25 #include "librpc/gen_ndr/ndr_lsa.h"
26 #include "librpc/gen_ndr/ndr_lsa_c.h"
27 #include "librpc/gen_ndr/ndr_samr.h"
28 #include "librpc/gen_ndr/ndr_samr_c.h"
29 #include "librpc/gen_ndr/ndr_netlogon.h"
30 #include "librpc/gen_ndr/ndr_netlogon_c.h"
31 #include "lib/cmdline/popt_common.h"
32 #include "librpc/rpc/dcerpc.h"
33 #include "torture/rpc/rpc.h"
34 #include "libcli/libcli.h"
35 #include "libcli/composite/composite.h"
36 #include "libcli/smb_composite/smb_composite.h"
37 #include "libcli/auth/libcli_auth.h"
38 #include "libcli/auth/credentials.h"
39 #include "lib/crypto/crypto.h"
42 * This tests a RPC call using an invalid vuid
45 BOOL torture_bind_authcontext(struct torture_context *torture)
50 struct lsa_ObjectAttribute objectattr;
51 struct lsa_OpenPolicy2 openpolicy;
52 struct policy_handle handle;
53 struct lsa_Close close;
54 struct smbcli_session *tmp;
55 struct smbcli_session *session2;
56 struct smbcli_state *cli;
57 struct dcerpc_pipe *lsa_pipe;
58 struct cli_credentials *anon_creds;
59 struct smb_composite_sesssetup setup;
61 mem_ctx = talloc_init("torture_bind_authcontext");
63 if (mem_ctx == NULL) {
64 d_printf("talloc_init failed\n");
68 status = smbcli_full_connection(mem_ctx, &cli,
69 lp_parm_string(-1, "torture", "host"),
70 "IPC$", NULL, cmdline_credentials,
72 if (!NT_STATUS_IS_OK(status)) {
73 d_printf("smbcli_full_connection failed: %s\n",
78 lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx);
79 if (lsa_pipe == NULL) {
80 d_printf("dcerpc_pipe_init failed\n");
84 status = dcerpc_pipe_open_smb(lsa_pipe->conn, cli->tree, "\\lsarpc");
85 if (!NT_STATUS_IS_OK(status)) {
86 d_printf("dcerpc_pipe_open_smb failed: %s\n",
91 status = dcerpc_bind_auth_none(lsa_pipe, &dcerpc_table_lsarpc);
92 if (!NT_STATUS_IS_OK(status)) {
93 d_printf("dcerpc_bind_auth_none failed: %s\n",
98 openpolicy.in.system_name =talloc_asprintf(
99 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
100 ZERO_STRUCT(objectattr);
101 openpolicy.in.attr = &objectattr;
102 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
103 openpolicy.out.handle = &handle;
105 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
107 if (!NT_STATUS_IS_OK(status)) {
108 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
113 close.in.handle = &handle;
114 close.out.handle = &handle;
116 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close);
117 if (!NT_STATUS_IS_OK(status)) {
118 d_printf("dcerpc_lsa_Close failed: %s\n",
123 session2 = smbcli_session_init(cli->transport, mem_ctx, False);
124 if (session2 == NULL) {
125 d_printf("smbcli_session_init failed\n");
129 anon_creds = cli_credentials_init(mem_ctx);
130 if (anon_creds == NULL) {
131 d_printf("cli_credentials_init failed\n");
135 cli_credentials_set_conf(anon_creds);
136 cli_credentials_set_anonymous(anon_creds);
138 setup.in.sesskey = cli->transport->negotiate.sesskey;
139 setup.in.capabilities = cli->transport->negotiate.capabilities;
140 setup.in.workgroup = "";
141 setup.in.credentials = anon_creds;
143 status = smb_composite_sesssetup(session2, &setup);
144 if (!NT_STATUS_IS_OK(status)) {
145 d_printf("anon session setup failed: %s\n",
150 tmp = cli->tree->session;
151 cli->tree->session = session2;
153 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
155 cli->tree->session = tmp;
156 talloc_free(lsa_pipe);
159 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
160 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
161 "expected NT_STATUS_INVALID_HANDLE\n",
168 talloc_free(mem_ctx);
173 * Bind to lsa using a specific auth method
176 static BOOL bindtest(struct smbcli_state *cli,
177 struct cli_credentials *credentials,
178 uint8_t auth_type, uint8_t auth_level)
184 struct dcerpc_pipe *lsa_pipe;
185 struct lsa_ObjectAttribute objectattr;
186 struct lsa_OpenPolicy2 openpolicy;
187 struct lsa_QueryInfoPolicy query;
188 struct policy_handle handle;
189 struct lsa_Close close;
191 if ((mem_ctx = talloc_init("bindtest")) == NULL) {
192 d_printf("talloc_init failed\n");
196 lsa_pipe = dcerpc_pipe_init(mem_ctx,
197 cli->transport->socket->event.ctx);
198 if (lsa_pipe == NULL) {
199 d_printf("dcerpc_pipe_init failed\n");
203 status = dcerpc_pipe_open_smb(lsa_pipe->conn, cli->tree, "\\lsarpc");
204 if (!NT_STATUS_IS_OK(status)) {
205 d_printf("dcerpc_pipe_open_smb failed: %s\n",
210 status = dcerpc_bind_auth(lsa_pipe, &dcerpc_table_lsarpc,
211 credentials, auth_type, auth_level,
213 if (!NT_STATUS_IS_OK(status)) {
214 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
218 openpolicy.in.system_name =talloc_asprintf(
219 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
220 ZERO_STRUCT(objectattr);
221 openpolicy.in.attr = &objectattr;
222 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
223 openpolicy.out.handle = &handle;
225 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
227 if (!NT_STATUS_IS_OK(status)) {
228 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
233 query.in.handle = &handle;
234 query.in.level = LSA_POLICY_INFO_DOMAIN;
236 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
237 if (!NT_STATUS_IS_OK(status)) {
238 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
243 close.in.handle = &handle;
244 close.out.handle = &handle;
246 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close);
247 if (!NT_STATUS_IS_OK(status)) {
248 d_printf("dcerpc_lsa_Close failed: %s\n",
255 talloc_free(mem_ctx);
260 * test authenticated RPC binds with the variants Samba3 does support
263 BOOL torture_bind_samba3(struct torture_context *torture)
268 struct smbcli_state *cli;
270 mem_ctx = talloc_init("torture_bind_authcontext");
272 if (mem_ctx == NULL) {
273 d_printf("talloc_init failed\n");
277 status = smbcli_full_connection(mem_ctx, &cli,
278 lp_parm_string(-1, "torture", "host"),
279 "IPC$", NULL, cmdline_credentials,
281 if (!NT_STATUS_IS_OK(status)) {
282 d_printf("smbcli_full_connection failed: %s\n",
289 ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
290 DCERPC_AUTH_LEVEL_INTEGRITY);
291 ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
292 DCERPC_AUTH_LEVEL_PRIVACY);
293 ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
294 DCERPC_AUTH_LEVEL_INTEGRITY);
295 ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
296 DCERPC_AUTH_LEVEL_PRIVACY);
299 talloc_free(mem_ctx);
304 * Lookup or create a user and return all necessary info
307 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
309 struct cli_credentials *admin_creds,
312 const char *wks_name,
314 struct dcerpc_pipe **result_pipe,
315 struct policy_handle **result_handle)
317 struct dcerpc_pipe *samr_pipe;
319 struct policy_handle conn_handle;
320 struct policy_handle domain_handle;
321 struct policy_handle *user_handle;
322 struct samr_Connect2 conn;
323 struct samr_EnumDomains enumdom;
324 uint32_t resume_handle = 0;
325 struct samr_LookupDomain l;
327 struct lsa_String domain_name;
328 struct lsa_String user_name;
329 struct samr_OpenDomain o;
330 struct samr_CreateUser2 c;
331 uint32_t user_rid,access_granted;
333 samr_pipe = dcerpc_pipe_init(mem_ctx,
334 cli->transport->socket->event.ctx);
335 if (samr_pipe == NULL) {
336 d_printf("dcerpc_pipe_init failed\n");
337 status = NT_STATUS_NO_MEMORY;
341 status = dcerpc_pipe_open_smb(samr_pipe->conn, cli->tree, "\\samr");
342 if (!NT_STATUS_IS_OK(status)) {
343 d_printf("dcerpc_pipe_open_smb failed: %s\n",
348 if (admin_creds != NULL) {
349 status = dcerpc_bind_auth(samr_pipe, &dcerpc_table_samr,
350 admin_creds, auth_type, auth_level,
352 if (!NT_STATUS_IS_OK(status)) {
353 d_printf("dcerpc_bind_auth failed: %s\n",
358 /* We must have an authenticated SMB connection */
359 status = dcerpc_bind_auth_none(samr_pipe, &dcerpc_table_samr);
360 if (!NT_STATUS_IS_OK(status)) {
361 d_printf("dcerpc_bind_auth_none failed: %s\n",
367 conn.in.system_name = talloc_asprintf(
368 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
369 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
370 conn.out.connect_handle = &conn_handle;
372 status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
373 if (!NT_STATUS_IS_OK(status)) {
374 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
378 enumdom.in.connect_handle = &conn_handle;
379 enumdom.in.resume_handle = &resume_handle;
380 enumdom.in.buf_size = (uint32_t)-1;
381 enumdom.out.resume_handle = &resume_handle;
383 status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
384 if (!NT_STATUS_IS_OK(status)) {
385 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
389 if (enumdom.out.num_entries != 2) {
390 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
391 enumdom.out.num_entries);
392 status = NT_STATUS_UNSUCCESSFUL;
396 dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
399 l.in.connect_handle = &conn_handle;
400 domain_name.string = enumdom.out.sam->entries[0].name.string;
401 *domain = talloc_strdup(mem_ctx, domain_name.string);
402 l.in.domain_name = &domain_name;
404 status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
405 if (!NT_STATUS_IS_OK(status)) {
406 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
410 o.in.connect_handle = &conn_handle;
411 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
412 o.in.sid = l.out.sid;
413 o.out.domain_handle = &domain_handle;
415 status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
416 if (!NT_STATUS_IS_OK(status)) {
417 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
421 c.in.domain_handle = &domain_handle;
422 user_name.string = talloc_asprintf(mem_ctx, "%s$", wks_name);
423 c.in.account_name = &user_name;
424 c.in.acct_flags = ACB_WSTRUST;
425 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
426 user_handle = talloc(mem_ctx, struct policy_handle);
427 c.out.user_handle = user_handle;
428 c.out.access_granted = &access_granted;
429 c.out.rid = &user_rid;
431 status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
433 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
434 struct samr_LookupNames ln;
435 struct samr_OpenUser ou;
437 ln.in.domain_handle = &domain_handle;
439 ln.in.names = &user_name;
441 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
442 if (!NT_STATUS_IS_OK(status)) {
443 d_printf("samr_LookupNames failed: %s\n",
448 ou.in.domain_handle = &domain_handle;
449 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
450 ou.in.rid = ln.out.rids.ids[0];
451 ou.out.user_handle = user_handle;
453 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
454 if (!NT_STATUS_IS_OK(status)) {
455 d_printf("samr_OpenUser failed: %s\n",
461 if (!NT_STATUS_IS_OK(status)) {
462 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
466 *result_pipe = samr_pipe;
467 *result_handle = user_handle;
475 * Do a Samba3-style join
478 static BOOL join3(struct smbcli_state *cli,
480 struct cli_credentials *admin_creds,
481 struct cli_credentials *wks_creds)
486 struct dcerpc_pipe *samr_pipe;
487 struct policy_handle *wks_handle;
490 if ((mem_ctx = talloc_init("join3")) == NULL) {
491 d_printf("talloc_init failed\n");
495 status = get_usr_handle(cli, mem_ctx, admin_creds,
496 DCERPC_AUTH_TYPE_NTLMSSP,
497 DCERPC_AUTH_LEVEL_PRIVACY,
498 cli_credentials_get_workstation(wks_creds),
499 &dom_name, &samr_pipe, &wks_handle);
501 if (!NT_STATUS_IS_OK(status)) {
502 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
506 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
509 struct samr_SetUserInfo2 sui2;
510 union samr_UserInfo u_info;
511 struct samr_UserInfo21 *i21 = &u_info.info25.info;
512 DATA_BLOB session_key;
513 DATA_BLOB confounded_session_key = data_blob_talloc(
515 struct MD5Context ctx;
516 uint8_t confounder[16];
520 i21->full_name.string = talloc_asprintf(
522 cli_credentials_get_workstation(wks_creds));
523 i21->acct_flags = ACB_WSTRUST;
524 i21->fields_present = SAMR_FIELD_FULL_NAME |
525 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
527 encode_pw_buffer(u_info.info25.password.data,
528 cli_credentials_get_password(wks_creds),
530 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
531 if (!NT_STATUS_IS_OK(status)) {
532 d_printf("dcerpc_fetch_session_key failed: %s\n",
536 generate_random_buffer((uint8_t *)confounder, 16);
539 MD5Update(&ctx, confounder, 16);
540 MD5Update(&ctx, session_key.data, session_key.length);
541 MD5Final(confounded_session_key.data, &ctx);
543 arcfour_crypt_blob(u_info.info25.password.data, 516,
544 &confounded_session_key);
545 memcpy(&u_info.info25.password.data[516], confounder, 16);
547 sui2.in.user_handle = wks_handle;
549 sui2.in.info = &u_info;
551 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
552 if (!NT_STATUS_IS_OK(status)) {
553 d_printf("samr_SetUserInfo2(25) failed: %s\n",
558 struct samr_SetUserInfo2 sui2;
559 struct samr_SetUserInfo sui;
560 union samr_UserInfo u_info;
561 DATA_BLOB session_key;
563 encode_pw_buffer(u_info.info24.password.data,
564 cli_credentials_get_password(wks_creds),
566 u_info.info24.pw_len =
567 strlen_m(cli_credentials_get_password(wks_creds))*2;
569 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
570 if (!NT_STATUS_IS_OK(status)) {
571 d_printf("dcerpc_fetch_session_key failed\n");
574 arcfour_crypt_blob(u_info.info24.password.data, 516,
576 sui2.in.user_handle = wks_handle;
577 sui2.in.info = &u_info;
580 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
581 if (!NT_STATUS_IS_OK(status)) {
582 d_printf("samr_SetUserInfo(24) failed\n");
586 u_info.info16.acct_flags = ACB_WSTRUST;
587 sui.in.user_handle = wks_handle;
588 sui.in.info = &u_info;
591 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
592 if (!NT_STATUS_IS_OK(status)) {
593 d_printf("samr_SetUserInfo(16) failed\n");
601 talloc_free(mem_ctx);
606 * Do a ReqChallenge/Auth2 and get the wks creds
609 static BOOL auth2(struct smbcli_state *cli,
610 struct cli_credentials *wks_cred)
613 struct dcerpc_pipe *net_pipe;
616 struct netr_ServerReqChallenge r;
617 struct netr_Credential netr_cli_creds;
618 struct netr_Credential netr_srv_creds;
619 uint32_t negotiate_flags;
620 struct netr_ServerAuthenticate2 a;
621 struct creds_CredentialState *creds_state;
622 struct netr_Credential netr_cred;
623 struct samr_Password mach_pw;
625 mem_ctx = talloc_new(NULL);
626 if (mem_ctx == NULL) {
627 d_printf("talloc_new failed\n");
631 net_pipe = dcerpc_pipe_init(mem_ctx,
632 cli->transport->socket->event.ctx);
633 if (net_pipe == NULL) {
634 d_printf("dcerpc_pipe_init failed\n");
638 status = dcerpc_pipe_open_smb(net_pipe->conn, cli->tree, "\\netlogon");
639 if (!NT_STATUS_IS_OK(status)) {
640 d_printf("dcerpc_pipe_open_smb failed: %s\n",
645 status = dcerpc_bind_auth_none(net_pipe, &dcerpc_table_netlogon);
646 if (!NT_STATUS_IS_OK(status)) {
647 d_printf("dcerpc_bind_auth_none failed: %s\n",
652 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
653 r.in.server_name = talloc_asprintf(
654 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
655 if (r.in.server_name == NULL) {
656 d_printf("talloc_asprintf failed\n");
659 generate_random_buffer(netr_cli_creds.data,
660 sizeof(netr_cli_creds.data));
661 r.in.credentials = &netr_cli_creds;
662 r.out.credentials = &netr_srv_creds;
664 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
665 if (!NT_STATUS_IS_OK(status)) {
666 d_printf("netr_ServerReqChallenge failed: %s\n",
671 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
672 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
674 creds_state = talloc(mem_ctx, struct creds_CredentialState);
675 creds_client_init(creds_state, r.in.credentials,
676 r.out.credentials, &mach_pw,
677 &netr_cred, negotiate_flags);
679 a.in.server_name = talloc_asprintf(
680 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
681 a.in.account_name = talloc_asprintf(
682 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
683 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
684 a.in.secure_channel_type = SEC_CHAN_WKSTA;
685 a.in.negotiate_flags = &negotiate_flags;
686 a.out.negotiate_flags = &negotiate_flags;
687 a.in.credentials = &netr_cred;
688 a.out.credentials = &netr_cred;
690 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
691 if (!NT_STATUS_IS_OK(status)) {
692 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
697 if (!creds_client_check(creds_state, a.out.credentials)) {
698 d_printf("creds_client_check failed\n");
702 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
707 talloc_free(mem_ctx);
712 * Do a couple of channel protected Netlogon ops: Interactive and Network
713 * login, and change the wks password
716 static BOOL schan(struct smbcli_state *cli,
717 struct cli_credentials *wks_creds,
718 struct cli_credentials *user_creds)
723 struct dcerpc_pipe *net_pipe;
726 mem_ctx = talloc_new(NULL);
727 if (mem_ctx == NULL) {
728 d_printf("talloc_new failed\n");
732 net_pipe = dcerpc_pipe_init(mem_ctx,
733 cli->transport->socket->event.ctx);
734 if (net_pipe == NULL) {
735 d_printf("dcerpc_pipe_init failed\n");
739 status = dcerpc_pipe_open_smb(net_pipe->conn, cli->tree, "\\netlogon");
740 if (!NT_STATUS_IS_OK(status)) {
741 d_printf("dcerpc_pipe_open_smb failed: %s\n",
747 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
748 DCERPC_DEBUG_PRINT_OUT;
751 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
752 status = dcerpc_bind_auth(net_pipe, &dcerpc_table_netlogon,
753 wks_creds, DCERPC_AUTH_TYPE_SCHANNEL,
754 DCERPC_AUTH_LEVEL_PRIVACY,
757 status = dcerpc_bind_auth_none(net_pipe, &dcerpc_table_netlogon);
759 if (!NT_STATUS_IS_OK(status)) {
760 d_printf("schannel bind failed: %s\n", nt_errstr(status));
765 for (i=2; i<4; i++) {
767 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
768 struct creds_CredentialState *creds_state;
769 struct netr_Authenticator netr_auth, netr_auth2;
770 struct netr_NetworkInfo ninfo;
771 struct netr_PasswordInfo pinfo;
772 struct netr_LogonSamLogon r;
774 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
775 CLI_CRED_NTLMv2_AUTH;
777 chal = data_blob_talloc(mem_ctx, NULL, 8);
778 if (chal.data == NULL) {
779 d_printf("data_blob_talloc failed\n");
783 generate_random_buffer(chal.data, chal.length);
784 names_blob = NTLMv2_generate_names_blob(
785 mem_ctx, cli_credentials_get_workstation(user_creds),
786 cli_credentials_get_domain(user_creds));
787 status = cli_credentials_get_ntlm_response(
788 user_creds, mem_ctx, &flags, chal, names_blob,
789 &lm_resp, &nt_resp, NULL, NULL);
790 if (!NT_STATUS_IS_OK(status)) {
791 d_printf("cli_credentials_get_ntlm_response failed:"
792 " %s\n", nt_errstr(status));
796 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
797 creds_client_authenticator(creds_state, &netr_auth);
799 ninfo.identity_info.account_name.string =
800 cli_credentials_get_username(user_creds);
801 ninfo.identity_info.domain_name.string =
802 cli_credentials_get_domain(user_creds);
803 ninfo.identity_info.parameter_control = 0;
804 ninfo.identity_info.logon_id_low = 0;
805 ninfo.identity_info.logon_id_high = 0;
806 ninfo.identity_info.workstation.string =
807 cli_credentials_get_workstation(user_creds);
808 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
809 ninfo.nt.length = nt_resp.length;
810 ninfo.nt.data = nt_resp.data;
811 ninfo.lm.length = lm_resp.length;
812 ninfo.lm.data = lm_resp.data;
814 r.in.server_name = talloc_asprintf(
815 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
816 ZERO_STRUCT(netr_auth2);
818 cli_credentials_get_workstation(wks_creds);
819 r.in.credential = &netr_auth;
820 r.in.return_authenticator = &netr_auth2;
821 r.in.logon_level = 2;
822 r.in.validation_level = i;
823 r.in.logon.network = &ninfo;
824 r.out.return_authenticator = NULL;
826 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
827 if (!NT_STATUS_IS_OK(status)) {
828 d_printf("netr_LogonSamLogon failed: %s\n",
833 if ((r.out.return_authenticator == NULL) ||
834 (!creds_client_check(creds_state,
835 &r.out.return_authenticator->cred))) {
836 d_printf("Credentials check failed!\n");
840 creds_client_authenticator(creds_state, &netr_auth);
842 pinfo.identity_info = ninfo.identity_info;
843 ZERO_STRUCT(pinfo.lmpassword.hash);
844 E_md4hash(cli_credentials_get_password(user_creds),
845 pinfo.ntpassword.hash);
846 session_key = data_blob_talloc(mem_ctx,
847 creds_state->session_key, 16);
848 arcfour_crypt_blob(pinfo.ntpassword.hash,
849 sizeof(pinfo.ntpassword.hash),
852 r.in.logon_level = 1;
853 r.in.logon.password = &pinfo;
854 r.out.return_authenticator = NULL;
856 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
857 if (!NT_STATUS_IS_OK(status)) {
858 d_printf("netr_LogonSamLogon failed: %s\n",
863 if ((r.out.return_authenticator == NULL) ||
864 (!creds_client_check(creds_state,
865 &r.out.return_authenticator->cred))) {
866 d_printf("Credentials check failed!\n");
872 struct netr_ServerPasswordSet s;
873 char *password = generate_random_str(wks_creds, 8);
874 struct creds_CredentialState *creds_state;
876 s.in.server_name = talloc_asprintf(
877 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
878 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
879 s.in.account_name = talloc_asprintf(
880 mem_ctx, "%s$", s.in.computer_name);
881 s.in.secure_channel_type = SEC_CHAN_WKSTA;
882 E_md4hash(password, s.in.new_password.hash);
883 creds_des_encrypt(creds_state, &s.in.new_password);
885 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
886 creds_client_authenticator(creds_state, &s.in.credential);
888 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
889 if (!NT_STATUS_IS_OK(status)) {
890 printf("ServerPasswordSet - %s\n", nt_errstr(status));
894 if (!creds_client_check(creds_state,
895 &s.out.return_authenticator.cred)) {
896 printf("Credential chaining failed\n");
899 cli_credentials_set_password(wks_creds, password,
905 talloc_free(mem_ctx);
910 * Delete the wks account again
913 static BOOL leave(struct smbcli_state *cli,
914 struct cli_credentials *admin_creds,
915 struct cli_credentials *wks_creds)
920 struct dcerpc_pipe *samr_pipe;
921 struct policy_handle *wks_handle;
924 if ((mem_ctx = talloc_init("leave")) == NULL) {
925 d_printf("talloc_init failed\n");
929 status = get_usr_handle(cli, mem_ctx, admin_creds,
930 DCERPC_AUTH_TYPE_NTLMSSP,
931 DCERPC_AUTH_LEVEL_PRIVACY,
932 cli_credentials_get_workstation(wks_creds),
933 &dom_name, &samr_pipe, &wks_handle);
935 if (!NT_STATUS_IS_OK(status)) {
936 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
941 struct samr_DeleteUser d;
943 d.in.user_handle = wks_handle;
944 d.out.user_handle = wks_handle;
946 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
947 if (!NT_STATUS_IS_OK(status)) {
948 d_printf("samr_DeleteUser failed\n");
956 talloc_free(mem_ctx);
961 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
964 BOOL torture_netlogon_samba3(struct torture_context *torture)
969 struct smbcli_state *cli;
970 struct cli_credentials *anon_creds;
971 struct cli_credentials *wks_creds;
972 const char *wks_name;
975 wks_name = lp_parm_string(-1, "torture", "wksname");
976 if (wks_name == NULL) {
977 wks_name = get_myname();
980 mem_ctx = talloc_init("torture_bind_authcontext");
982 if (mem_ctx == NULL) {
983 d_printf("talloc_init failed\n");
987 anon_creds = cli_credentials_init(mem_ctx);
988 if (anon_creds == NULL) {
989 d_printf("cli_credentials_init failed\n");
993 cli_credentials_set_conf(anon_creds);
994 cli_credentials_set_anonymous(anon_creds);
996 status = smbcli_full_connection(mem_ctx, &cli,
997 lp_parm_string(-1, "torture", "host"),
998 "IPC$", NULL, anon_creds, NULL);
999 if (!NT_STATUS_IS_OK(status)) {
1000 d_printf("smbcli_full_connection failed: %s\n",
1005 wks_creds = cli_credentials_init(mem_ctx);
1006 if (wks_creds == NULL) {
1007 d_printf("cli_credentials_init failed\n");
1011 cli_credentials_set_conf(wks_creds);
1012 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1013 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1014 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1015 cli_credentials_set_password(wks_creds, "", CRED_SPECIFIED);
1017 if (!join3(cli, False, cmdline_credentials, wks_creds)) {
1018 d_printf("join failed\n");
1022 cli_credentials_set_domain(
1023 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1026 for (i=0; i<2; i++) {
1028 /* Do this more than once, the routine "schan" changes
1029 * the workstation password using the netlogon
1030 * password change routine */
1034 if (!auth2(cli, wks_creds)) {
1035 d_printf("auth2 failed\n");
1039 for (j=0; j<2; j++) {
1040 if (!schan(cli, wks_creds, cmdline_credentials)) {
1041 d_printf("schan failed\n");
1047 if (!leave(cli, cmdline_credentials, wks_creds)) {
1048 d_printf("leave failed\n");
1055 talloc_free(mem_ctx);
1060 * Do a simple join, testjoin and leave using specified smb and samr
1064 static BOOL test_join3(TALLOC_CTX *mem_ctx,
1066 struct cli_credentials *smb_creds,
1067 struct cli_credentials *samr_creds,
1068 const char *wks_name)
1072 struct smbcli_state *cli;
1073 struct cli_credentials *wks_creds;
1075 status = smbcli_full_connection(mem_ctx, &cli,
1076 lp_parm_string(-1, "torture", "host"),
1077 "IPC$", NULL, smb_creds, NULL);
1078 if (!NT_STATUS_IS_OK(status)) {
1079 d_printf("smbcli_full_connection failed: %s\n",
1084 wks_creds = cli_credentials_init(cli);
1085 if (wks_creds == NULL) {
1086 d_printf("cli_credentials_init failed\n");
1090 cli_credentials_set_conf(wks_creds);
1091 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1092 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1093 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1094 cli_credentials_set_password(wks_creds,
1095 generate_random_str(wks_creds, 8),
1098 if (!join3(cli, use_level25, samr_creds, wks_creds)) {
1099 d_printf("join failed\n");
1103 cli_credentials_set_domain(
1104 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1107 if (!auth2(cli, wks_creds)) {
1108 d_printf("auth2 failed\n");
1112 if (!leave(cli, samr_creds, wks_creds)) {
1113 d_printf("leave failed\n");
1126 * Test the different session key variants. Do it by joining, this uses the
1127 * session key in the setpassword routine. Test the join by doing the auth2.
1130 BOOL torture_samba3_sessionkey(struct torture_context *torture)
1132 TALLOC_CTX *mem_ctx;
1134 struct cli_credentials *anon_creds;
1135 const char *wks_name;
1137 wks_name = lp_parm_string(-1, "torture", "wksname");
1138 if (wks_name == NULL) {
1139 wks_name = get_myname();
1142 mem_ctx = talloc_init("torture_bind_authcontext");
1144 if (mem_ctx == NULL) {
1145 d_printf("talloc_init failed\n");
1149 anon_creds = cli_credentials_init(mem_ctx);
1150 if (anon_creds == NULL) {
1151 d_printf("cli_credentials_init failed\n");
1155 cli_credentials_set_conf(anon_creds);
1156 cli_credentials_set_anonymous(anon_creds);
1160 if (!lp_parm_bool(-1, "target", "samba3", False)) {
1162 /* Samba3 in the build farm right now does this happily. Need
1165 if (test_join3(mem_ctx, False, anon_creds, NULL, wks_name)) {
1166 d_printf("join using anonymous bind on an anonymous smb "
1167 "connection succeeded -- HUH??\n");
1172 if (!test_join3(mem_ctx, False, anon_creds, cmdline_credentials,
1174 d_printf("join using ntlmssp bind on an anonymous smb "
1175 "connection failed\n");
1179 if (!test_join3(mem_ctx, False, cmdline_credentials, NULL, wks_name)) {
1180 d_printf("join using anonymous bind on an authenticated smb "
1181 "connection failed\n");
1185 if (!test_join3(mem_ctx, False, cmdline_credentials,
1186 cmdline_credentials,
1188 d_printf("join using ntlmssp bind on an authenticated smb "
1189 "connection failed\n");
1194 * The following two are tests for setuserinfolevel 25
1197 if (!test_join3(mem_ctx, True, anon_creds, cmdline_credentials,
1199 d_printf("join using ntlmssp bind on an anonymous smb "
1200 "connection failed\n");
1204 if (!test_join3(mem_ctx, True, cmdline_credentials, NULL, wks_name)) {
1205 d_printf("join using anonymous bind on an authenticated smb "
1206 "connection failed\n");