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 "libcli/raw/libcliraw.h"
25 #include "torture/torture.h"
26 #include "torture/util.h"
27 #include "librpc/gen_ndr/ndr_lsa.h"
28 #include "librpc/gen_ndr/ndr_lsa_c.h"
29 #include "librpc/gen_ndr/ndr_samr.h"
30 #include "librpc/gen_ndr/ndr_samr_c.h"
31 #include "librpc/gen_ndr/ndr_netlogon.h"
32 #include "librpc/gen_ndr/ndr_netlogon_c.h"
33 #include "librpc/gen_ndr/ndr_srvsvc.h"
34 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
35 #include "lib/cmdline/popt_common.h"
36 #include "librpc/rpc/dcerpc.h"
37 #include "torture/rpc/rpc.h"
38 #include "libcli/libcli.h"
39 #include "libcli/composite/composite.h"
40 #include "libcli/smb_composite/smb_composite.h"
41 #include "libcli/auth/libcli_auth.h"
42 #include "libcli/auth/credentials.h"
43 #include "lib/crypto/crypto.h"
44 #include "libcli/security/proto.h"
46 static struct cli_credentials *create_anon_creds(TALLOC_CTX *mem_ctx)
48 struct cli_credentials *result;
50 if (!(result = cli_credentials_init(mem_ctx))) {
54 cli_credentials_set_conf(result);
55 cli_credentials_set_anonymous(result);
61 * This tests a RPC call using an invalid vuid
64 BOOL torture_bind_authcontext(struct torture_context *torture)
69 struct lsa_ObjectAttribute objectattr;
70 struct lsa_OpenPolicy2 openpolicy;
71 struct policy_handle handle;
72 struct lsa_Close close;
73 struct smbcli_session *tmp;
74 struct smbcli_session *session2;
75 struct smbcli_state *cli;
76 struct dcerpc_pipe *lsa_pipe;
77 struct cli_credentials *anon_creds;
78 struct smb_composite_sesssetup setup;
80 mem_ctx = talloc_init("torture_bind_authcontext");
82 if (mem_ctx == NULL) {
83 d_printf("talloc_init failed\n");
87 status = smbcli_full_connection(mem_ctx, &cli,
88 lp_parm_string(-1, "torture", "host"),
89 "IPC$", NULL, cmdline_credentials,
91 if (!NT_STATUS_IS_OK(status)) {
92 d_printf("smbcli_full_connection failed: %s\n",
97 lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx);
98 if (lsa_pipe == NULL) {
99 d_printf("dcerpc_pipe_init failed\n");
103 status = dcerpc_pipe_open_smb(lsa_pipe->conn, cli->tree, "\\lsarpc");
104 if (!NT_STATUS_IS_OK(status)) {
105 d_printf("dcerpc_pipe_open_smb failed: %s\n",
110 status = dcerpc_bind_auth_none(lsa_pipe, &dcerpc_table_lsarpc);
111 if (!NT_STATUS_IS_OK(status)) {
112 d_printf("dcerpc_bind_auth_none failed: %s\n",
117 openpolicy.in.system_name =talloc_asprintf(
118 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
119 ZERO_STRUCT(objectattr);
120 openpolicy.in.attr = &objectattr;
121 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
122 openpolicy.out.handle = &handle;
124 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
126 if (!NT_STATUS_IS_OK(status)) {
127 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
132 close.in.handle = &handle;
133 close.out.handle = &handle;
135 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close);
136 if (!NT_STATUS_IS_OK(status)) {
137 d_printf("dcerpc_lsa_Close failed: %s\n",
142 session2 = smbcli_session_init(cli->transport, mem_ctx, False);
143 if (session2 == NULL) {
144 d_printf("smbcli_session_init failed\n");
148 if (!(anon_creds = create_anon_creds(mem_ctx))) {
149 d_printf("create_anon_creds failed\n");
153 setup.in.sesskey = cli->transport->negotiate.sesskey;
154 setup.in.capabilities = cli->transport->negotiate.capabilities;
155 setup.in.workgroup = "";
156 setup.in.credentials = anon_creds;
158 status = smb_composite_sesssetup(session2, &setup);
159 if (!NT_STATUS_IS_OK(status)) {
160 d_printf("anon session setup failed: %s\n",
165 tmp = cli->tree->session;
166 cli->tree->session = session2;
168 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
170 cli->tree->session = tmp;
171 talloc_free(lsa_pipe);
174 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
175 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
176 "expected NT_STATUS_INVALID_HANDLE\n",
183 talloc_free(mem_ctx);
188 * Bind to lsa using a specific auth method
191 static BOOL bindtest(struct smbcli_state *cli,
192 struct cli_credentials *credentials,
193 uint8_t auth_type, uint8_t auth_level)
199 struct dcerpc_pipe *lsa_pipe;
200 struct lsa_ObjectAttribute objectattr;
201 struct lsa_OpenPolicy2 openpolicy;
202 struct lsa_QueryInfoPolicy query;
203 struct policy_handle handle;
204 struct lsa_Close close;
206 if ((mem_ctx = talloc_init("bindtest")) == NULL) {
207 d_printf("talloc_init failed\n");
211 lsa_pipe = dcerpc_pipe_init(mem_ctx,
212 cli->transport->socket->event.ctx);
213 if (lsa_pipe == NULL) {
214 d_printf("dcerpc_pipe_init failed\n");
218 status = dcerpc_pipe_open_smb(lsa_pipe->conn, cli->tree, "\\lsarpc");
219 if (!NT_STATUS_IS_OK(status)) {
220 d_printf("dcerpc_pipe_open_smb failed: %s\n",
225 status = dcerpc_bind_auth(lsa_pipe, &dcerpc_table_lsarpc,
226 credentials, auth_type, auth_level,
228 if (!NT_STATUS_IS_OK(status)) {
229 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
233 openpolicy.in.system_name =talloc_asprintf(
234 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
235 ZERO_STRUCT(objectattr);
236 openpolicy.in.attr = &objectattr;
237 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
238 openpolicy.out.handle = &handle;
240 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
242 if (!NT_STATUS_IS_OK(status)) {
243 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
248 query.in.handle = &handle;
249 query.in.level = LSA_POLICY_INFO_DOMAIN;
251 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
252 if (!NT_STATUS_IS_OK(status)) {
253 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
258 close.in.handle = &handle;
259 close.out.handle = &handle;
261 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close);
262 if (!NT_STATUS_IS_OK(status)) {
263 d_printf("dcerpc_lsa_Close failed: %s\n",
270 talloc_free(mem_ctx);
275 * test authenticated RPC binds with the variants Samba3 does support
278 BOOL torture_bind_samba3(struct torture_context *torture)
283 struct smbcli_state *cli;
285 mem_ctx = talloc_init("torture_bind_authcontext");
287 if (mem_ctx == NULL) {
288 d_printf("talloc_init failed\n");
292 status = smbcli_full_connection(mem_ctx, &cli,
293 lp_parm_string(-1, "torture", "host"),
294 "IPC$", NULL, cmdline_credentials,
296 if (!NT_STATUS_IS_OK(status)) {
297 d_printf("smbcli_full_connection failed: %s\n",
304 ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
305 DCERPC_AUTH_LEVEL_INTEGRITY);
306 ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
307 DCERPC_AUTH_LEVEL_PRIVACY);
308 ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
309 DCERPC_AUTH_LEVEL_INTEGRITY);
310 ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
311 DCERPC_AUTH_LEVEL_PRIVACY);
314 talloc_free(mem_ctx);
319 * Lookup or create a user and return all necessary info
322 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
324 struct cli_credentials *admin_creds,
327 const char *wks_name,
329 struct dcerpc_pipe **result_pipe,
330 struct policy_handle **result_handle)
332 struct dcerpc_pipe *samr_pipe;
334 struct policy_handle conn_handle;
335 struct policy_handle domain_handle;
336 struct policy_handle *user_handle;
337 struct samr_Connect2 conn;
338 struct samr_EnumDomains enumdom;
339 uint32_t resume_handle = 0;
340 struct samr_LookupDomain l;
342 struct lsa_String domain_name;
343 struct lsa_String user_name;
344 struct samr_OpenDomain o;
345 struct samr_CreateUser2 c;
346 uint32_t user_rid,access_granted;
348 samr_pipe = dcerpc_pipe_init(mem_ctx,
349 cli->transport->socket->event.ctx);
350 if (samr_pipe == NULL) {
351 d_printf("dcerpc_pipe_init failed\n");
352 status = NT_STATUS_NO_MEMORY;
356 status = dcerpc_pipe_open_smb(samr_pipe->conn, cli->tree, "\\samr");
357 if (!NT_STATUS_IS_OK(status)) {
358 d_printf("dcerpc_pipe_open_smb failed: %s\n",
363 if (admin_creds != NULL) {
364 status = dcerpc_bind_auth(samr_pipe, &dcerpc_table_samr,
365 admin_creds, auth_type, auth_level,
367 if (!NT_STATUS_IS_OK(status)) {
368 d_printf("dcerpc_bind_auth failed: %s\n",
373 /* We must have an authenticated SMB connection */
374 status = dcerpc_bind_auth_none(samr_pipe, &dcerpc_table_samr);
375 if (!NT_STATUS_IS_OK(status)) {
376 d_printf("dcerpc_bind_auth_none failed: %s\n",
382 conn.in.system_name = talloc_asprintf(
383 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
384 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
385 conn.out.connect_handle = &conn_handle;
387 status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
388 if (!NT_STATUS_IS_OK(status)) {
389 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
393 enumdom.in.connect_handle = &conn_handle;
394 enumdom.in.resume_handle = &resume_handle;
395 enumdom.in.buf_size = (uint32_t)-1;
396 enumdom.out.resume_handle = &resume_handle;
398 status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
399 if (!NT_STATUS_IS_OK(status)) {
400 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
404 if (enumdom.out.num_entries != 2) {
405 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
406 enumdom.out.num_entries);
407 status = NT_STATUS_UNSUCCESSFUL;
411 dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
414 l.in.connect_handle = &conn_handle;
415 domain_name.string = enumdom.out.sam->entries[0].name.string;
416 *domain = talloc_strdup(mem_ctx, domain_name.string);
417 l.in.domain_name = &domain_name;
419 status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
420 if (!NT_STATUS_IS_OK(status)) {
421 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
425 o.in.connect_handle = &conn_handle;
426 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
427 o.in.sid = l.out.sid;
428 o.out.domain_handle = &domain_handle;
430 status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
431 if (!NT_STATUS_IS_OK(status)) {
432 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
436 c.in.domain_handle = &domain_handle;
437 user_name.string = talloc_asprintf(mem_ctx, "%s$", wks_name);
438 c.in.account_name = &user_name;
439 c.in.acct_flags = ACB_WSTRUST;
440 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
441 user_handle = talloc(mem_ctx, struct policy_handle);
442 c.out.user_handle = user_handle;
443 c.out.access_granted = &access_granted;
444 c.out.rid = &user_rid;
446 status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
448 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
449 struct samr_LookupNames ln;
450 struct samr_OpenUser ou;
452 ln.in.domain_handle = &domain_handle;
454 ln.in.names = &user_name;
456 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
457 if (!NT_STATUS_IS_OK(status)) {
458 d_printf("samr_LookupNames failed: %s\n",
463 ou.in.domain_handle = &domain_handle;
464 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
465 ou.in.rid = ln.out.rids.ids[0];
466 ou.out.user_handle = user_handle;
468 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
469 if (!NT_STATUS_IS_OK(status)) {
470 d_printf("samr_OpenUser failed: %s\n",
476 if (!NT_STATUS_IS_OK(status)) {
477 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
481 *result_pipe = samr_pipe;
482 *result_handle = user_handle;
490 * Do a Samba3-style join
493 static BOOL join3(struct smbcli_state *cli,
495 struct cli_credentials *admin_creds,
496 struct cli_credentials *wks_creds)
501 struct dcerpc_pipe *samr_pipe;
502 struct policy_handle *wks_handle;
505 if ((mem_ctx = talloc_init("join3")) == NULL) {
506 d_printf("talloc_init failed\n");
510 status = get_usr_handle(cli, mem_ctx, admin_creds,
511 DCERPC_AUTH_TYPE_NTLMSSP,
512 DCERPC_AUTH_LEVEL_PRIVACY,
513 cli_credentials_get_workstation(wks_creds),
514 &dom_name, &samr_pipe, &wks_handle);
516 if (!NT_STATUS_IS_OK(status)) {
517 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
521 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
524 struct samr_SetUserInfo2 sui2;
525 union samr_UserInfo u_info;
526 struct samr_UserInfo21 *i21 = &u_info.info25.info;
527 DATA_BLOB session_key;
528 DATA_BLOB confounded_session_key = data_blob_talloc(
530 struct MD5Context ctx;
531 uint8_t confounder[16];
535 i21->full_name.string = talloc_asprintf(
537 cli_credentials_get_workstation(wks_creds));
538 i21->acct_flags = ACB_WSTRUST;
539 i21->fields_present = SAMR_FIELD_FULL_NAME |
540 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
542 encode_pw_buffer(u_info.info25.password.data,
543 cli_credentials_get_password(wks_creds),
545 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
546 if (!NT_STATUS_IS_OK(status)) {
547 d_printf("dcerpc_fetch_session_key failed: %s\n",
551 generate_random_buffer((uint8_t *)confounder, 16);
554 MD5Update(&ctx, confounder, 16);
555 MD5Update(&ctx, session_key.data, session_key.length);
556 MD5Final(confounded_session_key.data, &ctx);
558 arcfour_crypt_blob(u_info.info25.password.data, 516,
559 &confounded_session_key);
560 memcpy(&u_info.info25.password.data[516], confounder, 16);
562 sui2.in.user_handle = wks_handle;
564 sui2.in.info = &u_info;
566 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
567 if (!NT_STATUS_IS_OK(status)) {
568 d_printf("samr_SetUserInfo2(25) failed: %s\n",
573 struct samr_SetUserInfo2 sui2;
574 struct samr_SetUserInfo sui;
575 union samr_UserInfo u_info;
576 DATA_BLOB session_key;
578 encode_pw_buffer(u_info.info24.password.data,
579 cli_credentials_get_password(wks_creds),
581 u_info.info24.pw_len =
582 strlen_m(cli_credentials_get_password(wks_creds))*2;
584 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
585 if (!NT_STATUS_IS_OK(status)) {
586 d_printf("dcerpc_fetch_session_key failed\n");
589 arcfour_crypt_blob(u_info.info24.password.data, 516,
591 sui2.in.user_handle = wks_handle;
592 sui2.in.info = &u_info;
595 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
596 if (!NT_STATUS_IS_OK(status)) {
597 d_printf("samr_SetUserInfo(24) failed\n");
601 u_info.info16.acct_flags = ACB_WSTRUST;
602 sui.in.user_handle = wks_handle;
603 sui.in.info = &u_info;
606 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
607 if (!NT_STATUS_IS_OK(status)) {
608 d_printf("samr_SetUserInfo(16) failed\n");
616 talloc_free(mem_ctx);
621 * Do a ReqChallenge/Auth2 and get the wks creds
624 static BOOL auth2(struct smbcli_state *cli,
625 struct cli_credentials *wks_cred)
628 struct dcerpc_pipe *net_pipe;
631 struct netr_ServerReqChallenge r;
632 struct netr_Credential netr_cli_creds;
633 struct netr_Credential netr_srv_creds;
634 uint32_t negotiate_flags;
635 struct netr_ServerAuthenticate2 a;
636 struct creds_CredentialState *creds_state;
637 struct netr_Credential netr_cred;
638 struct samr_Password mach_pw;
640 mem_ctx = talloc_new(NULL);
641 if (mem_ctx == NULL) {
642 d_printf("talloc_new failed\n");
646 net_pipe = dcerpc_pipe_init(mem_ctx,
647 cli->transport->socket->event.ctx);
648 if (net_pipe == NULL) {
649 d_printf("dcerpc_pipe_init failed\n");
653 status = dcerpc_pipe_open_smb(net_pipe->conn, cli->tree, "\\netlogon");
654 if (!NT_STATUS_IS_OK(status)) {
655 d_printf("dcerpc_pipe_open_smb failed: %s\n",
660 status = dcerpc_bind_auth_none(net_pipe, &dcerpc_table_netlogon);
661 if (!NT_STATUS_IS_OK(status)) {
662 d_printf("dcerpc_bind_auth_none failed: %s\n",
667 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
668 r.in.server_name = talloc_asprintf(
669 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
670 if (r.in.server_name == NULL) {
671 d_printf("talloc_asprintf failed\n");
674 generate_random_buffer(netr_cli_creds.data,
675 sizeof(netr_cli_creds.data));
676 r.in.credentials = &netr_cli_creds;
677 r.out.credentials = &netr_srv_creds;
679 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
680 if (!NT_STATUS_IS_OK(status)) {
681 d_printf("netr_ServerReqChallenge failed: %s\n",
686 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
687 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
689 creds_state = talloc(mem_ctx, struct creds_CredentialState);
690 creds_client_init(creds_state, r.in.credentials,
691 r.out.credentials, &mach_pw,
692 &netr_cred, negotiate_flags);
694 a.in.server_name = talloc_asprintf(
695 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
696 a.in.account_name = talloc_asprintf(
697 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
698 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
699 a.in.secure_channel_type = SEC_CHAN_WKSTA;
700 a.in.negotiate_flags = &negotiate_flags;
701 a.out.negotiate_flags = &negotiate_flags;
702 a.in.credentials = &netr_cred;
703 a.out.credentials = &netr_cred;
705 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
706 if (!NT_STATUS_IS_OK(status)) {
707 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
712 if (!creds_client_check(creds_state, a.out.credentials)) {
713 d_printf("creds_client_check failed\n");
717 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
722 talloc_free(mem_ctx);
727 * Do a couple of channel protected Netlogon ops: Interactive and Network
728 * login, and change the wks password
731 static BOOL schan(struct smbcli_state *cli,
732 struct cli_credentials *wks_creds,
733 struct cli_credentials *user_creds)
738 struct dcerpc_pipe *net_pipe;
741 mem_ctx = talloc_new(NULL);
742 if (mem_ctx == NULL) {
743 d_printf("talloc_new failed\n");
747 net_pipe = dcerpc_pipe_init(mem_ctx,
748 cli->transport->socket->event.ctx);
749 if (net_pipe == NULL) {
750 d_printf("dcerpc_pipe_init failed\n");
754 status = dcerpc_pipe_open_smb(net_pipe->conn, cli->tree, "\\netlogon");
755 if (!NT_STATUS_IS_OK(status)) {
756 d_printf("dcerpc_pipe_open_smb failed: %s\n",
762 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
763 DCERPC_DEBUG_PRINT_OUT;
766 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
767 status = dcerpc_bind_auth(net_pipe, &dcerpc_table_netlogon,
768 wks_creds, DCERPC_AUTH_TYPE_SCHANNEL,
769 DCERPC_AUTH_LEVEL_PRIVACY,
772 status = dcerpc_bind_auth_none(net_pipe, &dcerpc_table_netlogon);
774 if (!NT_STATUS_IS_OK(status)) {
775 d_printf("schannel bind failed: %s\n", nt_errstr(status));
780 for (i=2; i<4; i++) {
782 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
783 struct creds_CredentialState *creds_state;
784 struct netr_Authenticator netr_auth, netr_auth2;
785 struct netr_NetworkInfo ninfo;
786 struct netr_PasswordInfo pinfo;
787 struct netr_LogonSamLogon r;
789 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
790 CLI_CRED_NTLMv2_AUTH;
792 chal = data_blob_talloc(mem_ctx, NULL, 8);
793 if (chal.data == NULL) {
794 d_printf("data_blob_talloc failed\n");
798 generate_random_buffer(chal.data, chal.length);
799 names_blob = NTLMv2_generate_names_blob(
800 mem_ctx, cli_credentials_get_workstation(user_creds),
801 cli_credentials_get_domain(user_creds));
802 status = cli_credentials_get_ntlm_response(
803 user_creds, mem_ctx, &flags, chal, names_blob,
804 &lm_resp, &nt_resp, NULL, NULL);
805 if (!NT_STATUS_IS_OK(status)) {
806 d_printf("cli_credentials_get_ntlm_response failed:"
807 " %s\n", nt_errstr(status));
811 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
812 creds_client_authenticator(creds_state, &netr_auth);
814 ninfo.identity_info.account_name.string =
815 cli_credentials_get_username(user_creds);
816 ninfo.identity_info.domain_name.string =
817 cli_credentials_get_domain(user_creds);
818 ninfo.identity_info.parameter_control = 0;
819 ninfo.identity_info.logon_id_low = 0;
820 ninfo.identity_info.logon_id_high = 0;
821 ninfo.identity_info.workstation.string =
822 cli_credentials_get_workstation(user_creds);
823 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
824 ninfo.nt.length = nt_resp.length;
825 ninfo.nt.data = nt_resp.data;
826 ninfo.lm.length = lm_resp.length;
827 ninfo.lm.data = lm_resp.data;
829 r.in.server_name = talloc_asprintf(
830 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
831 ZERO_STRUCT(netr_auth2);
833 cli_credentials_get_workstation(wks_creds);
834 r.in.credential = &netr_auth;
835 r.in.return_authenticator = &netr_auth2;
836 r.in.logon_level = 2;
837 r.in.validation_level = i;
838 r.in.logon.network = &ninfo;
839 r.out.return_authenticator = NULL;
841 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
842 if (!NT_STATUS_IS_OK(status)) {
843 d_printf("netr_LogonSamLogon failed: %s\n",
848 if ((r.out.return_authenticator == NULL) ||
849 (!creds_client_check(creds_state,
850 &r.out.return_authenticator->cred))) {
851 d_printf("Credentials check failed!\n");
855 creds_client_authenticator(creds_state, &netr_auth);
857 pinfo.identity_info = ninfo.identity_info;
858 ZERO_STRUCT(pinfo.lmpassword.hash);
859 E_md4hash(cli_credentials_get_password(user_creds),
860 pinfo.ntpassword.hash);
861 session_key = data_blob_talloc(mem_ctx,
862 creds_state->session_key, 16);
863 arcfour_crypt_blob(pinfo.ntpassword.hash,
864 sizeof(pinfo.ntpassword.hash),
867 r.in.logon_level = 1;
868 r.in.logon.password = &pinfo;
869 r.out.return_authenticator = NULL;
871 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
872 if (!NT_STATUS_IS_OK(status)) {
873 d_printf("netr_LogonSamLogon failed: %s\n",
878 if ((r.out.return_authenticator == NULL) ||
879 (!creds_client_check(creds_state,
880 &r.out.return_authenticator->cred))) {
881 d_printf("Credentials check failed!\n");
887 struct netr_ServerPasswordSet s;
888 char *password = generate_random_str(wks_creds, 8);
889 struct creds_CredentialState *creds_state;
891 s.in.server_name = talloc_asprintf(
892 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
893 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
894 s.in.account_name = talloc_asprintf(
895 mem_ctx, "%s$", s.in.computer_name);
896 s.in.secure_channel_type = SEC_CHAN_WKSTA;
897 E_md4hash(password, s.in.new_password.hash);
899 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
900 creds_des_encrypt(creds_state, &s.in.new_password);
901 creds_client_authenticator(creds_state, &s.in.credential);
903 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
904 if (!NT_STATUS_IS_OK(status)) {
905 printf("ServerPasswordSet - %s\n", nt_errstr(status));
909 if (!creds_client_check(creds_state,
910 &s.out.return_authenticator.cred)) {
911 printf("Credential chaining failed\n");
914 cli_credentials_set_password(wks_creds, password,
920 talloc_free(mem_ctx);
925 * Delete the wks account again
928 static BOOL leave(struct smbcli_state *cli,
929 struct cli_credentials *admin_creds,
930 struct cli_credentials *wks_creds)
935 struct dcerpc_pipe *samr_pipe;
936 struct policy_handle *wks_handle;
939 if ((mem_ctx = talloc_init("leave")) == NULL) {
940 d_printf("talloc_init failed\n");
944 status = get_usr_handle(cli, mem_ctx, admin_creds,
945 DCERPC_AUTH_TYPE_NTLMSSP,
946 DCERPC_AUTH_LEVEL_PRIVACY,
947 cli_credentials_get_workstation(wks_creds),
948 &dom_name, &samr_pipe, &wks_handle);
950 if (!NT_STATUS_IS_OK(status)) {
951 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
956 struct samr_DeleteUser d;
958 d.in.user_handle = wks_handle;
959 d.out.user_handle = wks_handle;
961 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
962 if (!NT_STATUS_IS_OK(status)) {
963 d_printf("samr_DeleteUser failed\n");
971 talloc_free(mem_ctx);
976 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
979 BOOL torture_netlogon_samba3(struct torture_context *torture)
984 struct smbcli_state *cli;
985 struct cli_credentials *anon_creds;
986 struct cli_credentials *wks_creds;
987 const char *wks_name;
990 wks_name = lp_parm_string(-1, "torture", "wksname");
991 if (wks_name == NULL) {
992 wks_name = get_myname();
995 mem_ctx = talloc_init("torture_bind_authcontext");
997 if (mem_ctx == NULL) {
998 d_printf("talloc_init failed\n");
1002 if (!(anon_creds = create_anon_creds(mem_ctx))) {
1003 d_printf("create_anon_creds failed\n");
1007 status = smbcli_full_connection(mem_ctx, &cli,
1008 lp_parm_string(-1, "torture", "host"),
1009 "IPC$", NULL, anon_creds, NULL);
1010 if (!NT_STATUS_IS_OK(status)) {
1011 d_printf("smbcli_full_connection failed: %s\n",
1016 wks_creds = cli_credentials_init(mem_ctx);
1017 if (wks_creds == NULL) {
1018 d_printf("cli_credentials_init failed\n");
1022 cli_credentials_set_conf(wks_creds);
1023 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1024 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1025 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1026 cli_credentials_set_password(wks_creds, "", CRED_SPECIFIED);
1028 if (!join3(cli, False, cmdline_credentials, wks_creds)) {
1029 d_printf("join failed\n");
1033 cli_credentials_set_domain(
1034 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1037 for (i=0; i<2; i++) {
1039 /* Do this more than once, the routine "schan" changes
1040 * the workstation password using the netlogon
1041 * password change routine */
1045 if (!auth2(cli, wks_creds)) {
1046 d_printf("auth2 failed\n");
1050 for (j=0; j<2; j++) {
1051 if (!schan(cli, wks_creds, cmdline_credentials)) {
1052 d_printf("schan failed\n");
1058 if (!leave(cli, cmdline_credentials, wks_creds)) {
1059 d_printf("leave failed\n");
1066 talloc_free(mem_ctx);
1071 * Do a simple join, testjoin and leave using specified smb and samr
1075 static BOOL test_join3(TALLOC_CTX *mem_ctx,
1077 struct cli_credentials *smb_creds,
1078 struct cli_credentials *samr_creds,
1079 const char *wks_name)
1083 struct smbcli_state *cli;
1084 struct cli_credentials *wks_creds;
1086 status = smbcli_full_connection(mem_ctx, &cli,
1087 lp_parm_string(-1, "torture", "host"),
1088 "IPC$", NULL, smb_creds, NULL);
1089 if (!NT_STATUS_IS_OK(status)) {
1090 d_printf("smbcli_full_connection failed: %s\n",
1095 wks_creds = cli_credentials_init(cli);
1096 if (wks_creds == NULL) {
1097 d_printf("cli_credentials_init failed\n");
1101 cli_credentials_set_conf(wks_creds);
1102 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1103 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1104 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1105 cli_credentials_set_password(wks_creds,
1106 generate_random_str(wks_creds, 8),
1109 if (!join3(cli, use_level25, samr_creds, wks_creds)) {
1110 d_printf("join failed\n");
1114 cli_credentials_set_domain(
1115 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1118 if (!auth2(cli, wks_creds)) {
1119 d_printf("auth2 failed\n");
1123 if (!leave(cli, samr_creds, wks_creds)) {
1124 d_printf("leave failed\n");
1137 * Test the different session key variants. Do it by joining, this uses the
1138 * session key in the setpassword routine. Test the join by doing the auth2.
1141 BOOL torture_samba3_sessionkey(struct torture_context *torture)
1143 TALLOC_CTX *mem_ctx;
1145 struct cli_credentials *anon_creds;
1146 const char *wks_name;
1148 wks_name = lp_parm_string(-1, "torture", "wksname");
1149 if (wks_name == NULL) {
1150 wks_name = get_myname();
1153 mem_ctx = talloc_init("torture_samba3_sessionkey");
1155 if (mem_ctx == NULL) {
1156 d_printf("talloc_init failed\n");
1160 if (!(anon_creds = create_anon_creds(mem_ctx))) {
1161 d_printf("create_anon_creds failed\n");
1167 if (!lp_parm_bool(-1, "target", "samba3", False)) {
1169 /* Samba3 in the build farm right now does this happily. Need
1172 if (test_join3(mem_ctx, False, anon_creds, NULL, wks_name)) {
1173 d_printf("join using anonymous bind on an anonymous smb "
1174 "connection succeeded -- HUH??\n");
1179 if (!test_join3(mem_ctx, False, anon_creds, cmdline_credentials,
1181 d_printf("join using ntlmssp bind on an anonymous smb "
1182 "connection failed\n");
1186 if (!test_join3(mem_ctx, False, cmdline_credentials, NULL, wks_name)) {
1187 d_printf("join using anonymous bind on an authenticated smb "
1188 "connection failed\n");
1192 if (!test_join3(mem_ctx, False, cmdline_credentials,
1193 cmdline_credentials,
1195 d_printf("join using ntlmssp bind on an authenticated smb "
1196 "connection failed\n");
1201 * The following two are tests for setuserinfolevel 25
1204 if (!test_join3(mem_ctx, True, anon_creds, cmdline_credentials,
1206 d_printf("join using ntlmssp bind on an anonymous smb "
1207 "connection failed\n");
1211 if (!test_join3(mem_ctx, True, cmdline_credentials, NULL, wks_name)) {
1212 d_printf("join using anonymous bind on an authenticated smb "
1213 "connection failed\n");
1223 * open pipe and bind, given an IPC$ context
1226 static struct dcerpc_pipe *pipe_bind_smb(TALLOC_CTX *mem_ctx,
1227 struct smbcli_tree *tree,
1228 const char *pipe_name,
1229 const struct dcerpc_interface_table *iface)
1231 struct dcerpc_pipe *result;
1234 if (!(result = dcerpc_pipe_init(
1235 mem_ctx, tree->session->transport->socket->event.ctx))) {
1239 status = dcerpc_pipe_open_smb(result->conn, tree, pipe_name);
1240 if (!NT_STATUS_IS_OK(status)) {
1241 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1243 talloc_free(result);
1247 status = dcerpc_bind_auth_none(result, iface);
1248 if (!NT_STATUS_IS_OK(status)) {
1249 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1250 talloc_free(result);
1258 * Sane wrapper around lsa_LookupNames
1261 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1262 struct dcerpc_pipe *p,
1266 struct lsa_ObjectAttribute attr;
1267 struct lsa_QosInfo qos;
1268 struct lsa_OpenPolicy2 r;
1271 struct policy_handle handle;
1272 struct lsa_LookupNames l;
1273 struct lsa_TransSidArray sids;
1274 struct lsa_String lsa_name;
1276 struct dom_sid *result;
1277 TALLOC_CTX *tmp_ctx;
1279 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1284 qos.impersonation_level = 2;
1285 qos.context_mode = 1;
1286 qos.effective_only = 0;
1289 attr.root_dir = NULL;
1290 attr.object_name = NULL;
1291 attr.attributes = 0;
1292 attr.sec_desc = NULL;
1293 attr.sec_qos = &qos;
1295 r.in.system_name = "\\";
1297 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1298 r.out.handle = &handle;
1300 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1301 if (!NT_STATUS_IS_OK(status)) {
1302 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1303 talloc_free(tmp_ctx);
1310 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1312 l.in.handle = &handle;
1314 l.in.names = &lsa_name;
1317 l.in.count = &count;
1318 l.out.count = &count;
1321 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1322 if (!NT_STATUS_IS_OK(status)) {
1323 printf("LookupNames failed - %s\n", nt_errstr(status));
1324 talloc_free(tmp_ctx);
1328 result = dom_sid_add_rid(mem_ctx, l.out.domains->domains[0].sid,
1329 l.out.sids->sids[0].rid);
1331 c.in.handle = &handle;
1332 c.out.handle = &handle;
1334 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1335 if (!NT_STATUS_IS_OK(status)) {
1336 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1337 talloc_free(tmp_ctx);
1341 talloc_free(tmp_ctx);
1346 * Find out the user SID on this connection
1349 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree)
1351 struct dcerpc_pipe *lsa;
1352 struct lsa_GetUserName r;
1354 struct lsa_StringPointer authority_name_p;
1355 struct dom_sid *result;
1357 if (!(lsa = pipe_bind_smb(mem_ctx, tree, "\\pipe\\lsarpc",
1358 &dcerpc_table_lsarpc))) {
1359 d_printf("Could not bind to LSA\n");
1363 r.in.system_name = "\\";
1364 r.in.account_name = NULL;
1365 authority_name_p.string = NULL;
1366 r.in.authority_name = &authority_name_p;
1368 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1370 if (!NT_STATUS_IS_OK(status)) {
1371 printf("GetUserName failed - %s\n", nt_errstr(status));
1376 result = name2sid(mem_ctx, lsa, r.out.account_name->string,
1377 r.out.authority_name->string->string);
1384 * Test the getusername behaviour
1387 BOOL torture_samba3_rpc_getusername(struct torture_context *torture)
1390 struct smbcli_state *cli;
1391 TALLOC_CTX *mem_ctx;
1393 struct dom_sid *user_sid;
1394 struct cli_credentials *anon_creds;
1396 if (!(mem_ctx = talloc_new(torture))) {
1400 status = smbcli_full_connection(
1401 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1402 "IPC$", NULL, cmdline_credentials, NULL);
1403 if (!NT_STATUS_IS_OK(status)) {
1404 d_printf("smbcli_full_connection failed: %s\n",
1410 if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1411 d_printf("whoami on auth'ed connection failed\n");
1417 if (!(anon_creds = create_anon_creds(mem_ctx))) {
1418 d_printf("create_anon_creds failed\n");
1423 status = smbcli_full_connection(
1424 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1425 "IPC$", NULL, anon_creds, NULL);
1426 if (!NT_STATUS_IS_OK(status)) {
1427 d_printf("anon smbcli_full_connection failed: %s\n",
1433 if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1434 d_printf("whoami on anon connection failed\n");
1439 if (!dom_sid_equal(user_sid,
1440 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1441 d_printf("Anon lsa_GetUserName returned %s, expected S-1-5-7",
1442 dom_sid_string(mem_ctx, user_sid));
1447 talloc_free(mem_ctx);
1451 static BOOL test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1452 const char *sharename)
1455 struct srvsvc_NetShareGetInfo r;
1456 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1460 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1461 dcerpc_server_name(p));
1462 r.in.share_name = sharename;
1464 for (i=0;i<ARRAY_SIZE(levels);i++) {
1465 r.in.level = levels[i];
1469 printf("testing NetShareGetInfo level %u on share '%s'\n",
1470 r.in.level, r.in.share_name);
1472 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1473 if (!NT_STATUS_IS_OK(status)) {
1474 printf("NetShareGetInfo level %u on share '%s' failed"
1475 " - %s\n", r.in.level, r.in.share_name,
1480 if (!W_ERROR_IS_OK(r.out.result)) {
1481 printf("NetShareGetInfo level %u on share '%s' failed "
1482 "- %s\n", r.in.level, r.in.share_name,
1483 win_errstr(r.out.result));
1492 static BOOL test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1493 const char **one_sharename)
1496 struct srvsvc_NetShareEnum r;
1497 struct srvsvc_NetShareCtr0 c0;
1498 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1502 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1503 r.in.ctr.ctr0 = &c0;
1504 r.in.ctr.ctr0->count = 0;
1505 r.in.ctr.ctr0->array = NULL;
1506 r.in.max_buffer = (uint32_t)-1;
1507 r.in.resume_handle = NULL;
1509 for (i=0;i<ARRAY_SIZE(levels);i++) {
1510 r.in.level = levels[i];
1514 printf("testing NetShareEnum level %u\n", r.in.level);
1515 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1516 if (!NT_STATUS_IS_OK(status)) {
1517 printf("NetShareEnum level %u failed - %s\n",
1518 r.in.level, nt_errstr(status));
1522 if (!W_ERROR_IS_OK(r.out.result)) {
1523 printf("NetShareEnum level %u failed - %s\n",
1524 r.in.level, win_errstr(r.out.result));
1527 if (r.in.level == 0) {
1528 struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1529 if (ctr->count > 0) {
1530 *one_sharename = ctr->array[0].name;
1538 BOOL torture_samba3_rpc_srvsvc(struct torture_context *torture)
1540 struct dcerpc_pipe *p;
1541 TALLOC_CTX *mem_ctx;
1543 const char *sharename = NULL;
1544 struct smbcli_state *cli;
1546 if (!(mem_ctx = talloc_new(torture))) {
1550 if (!(torture_open_connection_share(
1551 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1553 talloc_free(mem_ctx);
1557 if (!(p = pipe_bind_smb(mem_ctx, cli->tree, "\\pipe\\srvsvc",
1558 &dcerpc_table_srvsvc))) {
1559 d_printf("could not bind to srvsvc pipe\n");
1564 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1565 if (sharename == NULL) {
1566 printf("did not get sharename\n");
1568 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1571 talloc_free(mem_ctx);