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_handle;
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_handle.in.handle = &handle;
133 close_handle.out.handle = &handle;
135 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
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_handle;
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_handle.in.handle = &handle;
259 close_handle.out.handle = &handle;
261 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
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 *username,
329 struct dcerpc_pipe **result_pipe,
330 struct policy_handle **result_handle,
331 struct dom_sid **sid)
333 struct dcerpc_pipe *samr_pipe;
335 struct policy_handle conn_handle;
336 struct policy_handle domain_handle;
337 struct policy_handle *user_handle;
338 struct samr_Connect2 conn;
339 struct samr_EnumDomains enumdom;
340 uint32_t resume_handle = 0;
341 struct samr_LookupDomain l;
343 struct lsa_String domain_name;
344 struct lsa_String user_name;
345 struct samr_OpenDomain o;
346 struct samr_CreateUser2 c;
347 uint32_t user_rid,access_granted;
349 samr_pipe = dcerpc_pipe_init(mem_ctx,
350 cli->transport->socket->event.ctx);
351 if (samr_pipe == NULL) {
352 d_printf("dcerpc_pipe_init failed\n");
353 status = NT_STATUS_NO_MEMORY;
357 status = dcerpc_pipe_open_smb(samr_pipe->conn, cli->tree, "\\samr");
358 if (!NT_STATUS_IS_OK(status)) {
359 d_printf("dcerpc_pipe_open_smb failed: %s\n",
364 if (admin_creds != NULL) {
365 status = dcerpc_bind_auth(samr_pipe, &dcerpc_table_samr,
366 admin_creds, auth_type, auth_level,
368 if (!NT_STATUS_IS_OK(status)) {
369 d_printf("dcerpc_bind_auth failed: %s\n",
374 /* We must have an authenticated SMB connection */
375 status = dcerpc_bind_auth_none(samr_pipe, &dcerpc_table_samr);
376 if (!NT_STATUS_IS_OK(status)) {
377 d_printf("dcerpc_bind_auth_none failed: %s\n",
383 conn.in.system_name = talloc_asprintf(
384 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
385 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
386 conn.out.connect_handle = &conn_handle;
388 status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
389 if (!NT_STATUS_IS_OK(status)) {
390 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
394 enumdom.in.connect_handle = &conn_handle;
395 enumdom.in.resume_handle = &resume_handle;
396 enumdom.in.buf_size = (uint32_t)-1;
397 enumdom.out.resume_handle = &resume_handle;
399 status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
400 if (!NT_STATUS_IS_OK(status)) {
401 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
405 if (enumdom.out.num_entries != 2) {
406 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
407 enumdom.out.num_entries);
408 status = NT_STATUS_UNSUCCESSFUL;
412 dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
415 l.in.connect_handle = &conn_handle;
416 domain_name.string = enumdom.out.sam->entries[0].name.string;
417 *domain = talloc_strdup(mem_ctx, domain_name.string);
418 l.in.domain_name = &domain_name;
420 status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
421 if (!NT_STATUS_IS_OK(status)) {
422 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
426 o.in.connect_handle = &conn_handle;
427 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
428 o.in.sid = l.out.sid;
429 o.out.domain_handle = &domain_handle;
431 status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
432 if (!NT_STATUS_IS_OK(status)) {
433 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
437 c.in.domain_handle = &domain_handle;
438 user_name.string = username;
439 c.in.account_name = &user_name;
440 c.in.acct_flags = ACB_NORMAL;
441 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
442 user_handle = talloc(mem_ctx, struct policy_handle);
443 c.out.user_handle = user_handle;
444 c.out.access_granted = &access_granted;
445 c.out.rid = &user_rid;
447 status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
449 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
450 struct samr_LookupNames ln;
451 struct samr_OpenUser ou;
453 ln.in.domain_handle = &domain_handle;
455 ln.in.names = &user_name;
457 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
458 if (!NT_STATUS_IS_OK(status)) {
459 d_printf("samr_LookupNames failed: %s\n",
464 ou.in.domain_handle = &domain_handle;
465 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
466 user_rid = ou.in.rid = ln.out.rids.ids[0];
467 ou.out.user_handle = user_handle;
469 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
470 if (!NT_STATUS_IS_OK(status)) {
471 d_printf("samr_OpenUser failed: %s\n",
477 if (!NT_STATUS_IS_OK(status)) {
478 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
482 *result_pipe = samr_pipe;
483 *result_handle = user_handle;
485 *sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);
497 static BOOL create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
498 struct cli_credentials *admin_creds,
499 const char *username, const char *password,
501 struct dom_sid **user_sid)
505 struct dcerpc_pipe *samr_pipe;
506 struct policy_handle *wks_handle;
509 if (!(tmp_ctx = talloc_new(mem_ctx))) {
510 d_printf("talloc_init failed\n");
514 status = get_usr_handle(cli, tmp_ctx, admin_creds,
515 DCERPC_AUTH_TYPE_NTLMSSP,
516 DCERPC_AUTH_LEVEL_INTEGRITY,
517 username, domain_name, &samr_pipe, &wks_handle,
519 if (!NT_STATUS_IS_OK(status)) {
520 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
525 struct samr_SetUserInfo2 sui2;
526 struct samr_SetUserInfo sui;
527 struct samr_QueryUserInfo qui;
528 union samr_UserInfo u_info;
529 DATA_BLOB session_key;
531 encode_pw_buffer(u_info.info24.password.data, password,
533 u_info.info24.pw_len = strlen_m(password)*2;
535 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
536 if (!NT_STATUS_IS_OK(status)) {
537 d_printf("dcerpc_fetch_session_key failed\n");
540 arcfour_crypt_blob(u_info.info24.password.data, 516,
542 sui2.in.user_handle = wks_handle;
543 sui2.in.info = &u_info;
546 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
547 if (!NT_STATUS_IS_OK(status)) {
548 d_printf("samr_SetUserInfo(24) failed: %s\n",
553 u_info.info16.acct_flags = ACB_NORMAL;
554 sui.in.user_handle = wks_handle;
555 sui.in.info = &u_info;
558 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
559 if (!NT_STATUS_IS_OK(status)) {
560 d_printf("samr_SetUserInfo(16) failed\n");
564 qui.in.user_handle = wks_handle;
567 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
568 if (!NT_STATUS_IS_OK(status)) {
569 d_printf("samr_QueryUserInfo(21) failed\n");
573 qui.out.info->info21.allow_password_change = 0;
574 qui.out.info->info21.force_password_change = 0;
575 qui.out.info->info21.account_name.string = NULL;
576 qui.out.info->info21.rid = 0;
577 qui.out.info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
579 u_info.info21 = qui.out.info->info21;
580 sui.in.user_handle = wks_handle;
581 sui.in.info = &u_info;
584 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
585 if (!NT_STATUS_IS_OK(status)) {
586 d_printf("samr_SetUserInfo(21) failed\n");
591 *domain_name= talloc_steal(mem_ctx, *domain_name);
592 *user_sid = talloc_steal(mem_ctx, *user_sid);
595 talloc_free(tmp_ctx);
603 static BOOL delete_user(struct smbcli_state *cli,
604 struct cli_credentials *admin_creds,
605 const char *username)
610 struct dcerpc_pipe *samr_pipe;
611 struct policy_handle *user_handle;
614 if ((mem_ctx = talloc_init("leave")) == NULL) {
615 d_printf("talloc_init failed\n");
619 status = get_usr_handle(cli, mem_ctx, admin_creds,
620 DCERPC_AUTH_TYPE_NTLMSSP,
621 DCERPC_AUTH_LEVEL_INTEGRITY,
622 username, &dom_name, &samr_pipe,
625 if (!NT_STATUS_IS_OK(status)) {
626 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
631 struct samr_DeleteUser d;
633 d.in.user_handle = user_handle;
634 d.out.user_handle = user_handle;
636 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
637 if (!NT_STATUS_IS_OK(status)) {
638 d_printf("samr_DeleteUser failed\n");
646 talloc_free(mem_ctx);
651 * Do a Samba3-style join
654 static BOOL join3(struct smbcli_state *cli,
656 struct cli_credentials *admin_creds,
657 struct cli_credentials *wks_creds)
662 struct dcerpc_pipe *samr_pipe;
663 struct policy_handle *wks_handle;
666 if ((mem_ctx = talloc_init("join3")) == NULL) {
667 d_printf("talloc_init failed\n");
671 status = get_usr_handle(
672 cli, mem_ctx, admin_creds,
673 DCERPC_AUTH_TYPE_NTLMSSP,
674 DCERPC_AUTH_LEVEL_PRIVACY,
675 talloc_asprintf(mem_ctx, "%s$",
676 cli_credentials_get_workstation(wks_creds)),
677 &dom_name, &samr_pipe, &wks_handle, NULL);
679 if (!NT_STATUS_IS_OK(status)) {
680 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
684 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
687 struct samr_SetUserInfo2 sui2;
688 union samr_UserInfo u_info;
689 struct samr_UserInfo21 *i21 = &u_info.info25.info;
690 DATA_BLOB session_key;
691 DATA_BLOB confounded_session_key = data_blob_talloc(
693 struct MD5Context ctx;
694 uint8_t confounder[16];
698 i21->full_name.string = talloc_asprintf(
700 cli_credentials_get_workstation(wks_creds));
701 i21->acct_flags = ACB_WSTRUST;
702 i21->fields_present = SAMR_FIELD_FULL_NAME |
703 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
705 encode_pw_buffer(u_info.info25.password.data,
706 cli_credentials_get_password(wks_creds),
708 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
709 if (!NT_STATUS_IS_OK(status)) {
710 d_printf("dcerpc_fetch_session_key failed: %s\n",
714 generate_random_buffer((uint8_t *)confounder, 16);
717 MD5Update(&ctx, confounder, 16);
718 MD5Update(&ctx, session_key.data, session_key.length);
719 MD5Final(confounded_session_key.data, &ctx);
721 arcfour_crypt_blob(u_info.info25.password.data, 516,
722 &confounded_session_key);
723 memcpy(&u_info.info25.password.data[516], confounder, 16);
725 sui2.in.user_handle = wks_handle;
727 sui2.in.info = &u_info;
729 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
730 if (!NT_STATUS_IS_OK(status)) {
731 d_printf("samr_SetUserInfo2(25) failed: %s\n",
736 struct samr_SetUserInfo2 sui2;
737 struct samr_SetUserInfo sui;
738 union samr_UserInfo u_info;
739 DATA_BLOB session_key;
741 encode_pw_buffer(u_info.info24.password.data,
742 cli_credentials_get_password(wks_creds),
744 u_info.info24.pw_len =
745 strlen_m(cli_credentials_get_password(wks_creds))*2;
747 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
748 if (!NT_STATUS_IS_OK(status)) {
749 d_printf("dcerpc_fetch_session_key failed\n");
752 arcfour_crypt_blob(u_info.info24.password.data, 516,
754 sui2.in.user_handle = wks_handle;
755 sui2.in.info = &u_info;
758 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
759 if (!NT_STATUS_IS_OK(status)) {
760 d_printf("samr_SetUserInfo(24) failed: %s\n",
765 u_info.info16.acct_flags = ACB_WSTRUST;
766 sui.in.user_handle = wks_handle;
767 sui.in.info = &u_info;
770 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
771 if (!NT_STATUS_IS_OK(status)) {
772 d_printf("samr_SetUserInfo(16) failed\n");
780 talloc_free(mem_ctx);
785 * Do a ReqChallenge/Auth2 and get the wks creds
788 static BOOL auth2(struct smbcli_state *cli,
789 struct cli_credentials *wks_cred)
792 struct dcerpc_pipe *net_pipe;
795 struct netr_ServerReqChallenge r;
796 struct netr_Credential netr_cli_creds;
797 struct netr_Credential netr_srv_creds;
798 uint32_t negotiate_flags;
799 struct netr_ServerAuthenticate2 a;
800 struct creds_CredentialState *creds_state;
801 struct netr_Credential netr_cred;
802 struct samr_Password mach_pw;
804 mem_ctx = talloc_new(NULL);
805 if (mem_ctx == NULL) {
806 d_printf("talloc_new failed\n");
810 net_pipe = dcerpc_pipe_init(mem_ctx,
811 cli->transport->socket->event.ctx);
812 if (net_pipe == NULL) {
813 d_printf("dcerpc_pipe_init failed\n");
817 status = dcerpc_pipe_open_smb(net_pipe->conn, cli->tree, "\\netlogon");
818 if (!NT_STATUS_IS_OK(status)) {
819 d_printf("dcerpc_pipe_open_smb failed: %s\n",
824 status = dcerpc_bind_auth_none(net_pipe, &dcerpc_table_netlogon);
825 if (!NT_STATUS_IS_OK(status)) {
826 d_printf("dcerpc_bind_auth_none failed: %s\n",
831 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
832 r.in.server_name = talloc_asprintf(
833 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
834 if (r.in.server_name == NULL) {
835 d_printf("talloc_asprintf failed\n");
838 generate_random_buffer(netr_cli_creds.data,
839 sizeof(netr_cli_creds.data));
840 r.in.credentials = &netr_cli_creds;
841 r.out.credentials = &netr_srv_creds;
843 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
844 if (!NT_STATUS_IS_OK(status)) {
845 d_printf("netr_ServerReqChallenge failed: %s\n",
850 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
851 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
853 creds_state = talloc(mem_ctx, struct creds_CredentialState);
854 creds_client_init(creds_state, r.in.credentials,
855 r.out.credentials, &mach_pw,
856 &netr_cred, negotiate_flags);
858 a.in.server_name = talloc_asprintf(
859 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
860 a.in.account_name = talloc_asprintf(
861 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
862 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
863 a.in.secure_channel_type = SEC_CHAN_WKSTA;
864 a.in.negotiate_flags = &negotiate_flags;
865 a.out.negotiate_flags = &negotiate_flags;
866 a.in.credentials = &netr_cred;
867 a.out.credentials = &netr_cred;
869 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
870 if (!NT_STATUS_IS_OK(status)) {
871 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
876 if (!creds_client_check(creds_state, a.out.credentials)) {
877 d_printf("creds_client_check failed\n");
881 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
886 talloc_free(mem_ctx);
891 * Do a couple of schannel protected Netlogon ops: Interactive and Network
892 * login, and change the wks password
895 static BOOL schan(struct smbcli_state *cli,
896 struct cli_credentials *wks_creds,
897 struct cli_credentials *user_creds)
902 struct dcerpc_pipe *net_pipe;
905 mem_ctx = talloc_new(NULL);
906 if (mem_ctx == NULL) {
907 d_printf("talloc_new failed\n");
911 net_pipe = dcerpc_pipe_init(mem_ctx,
912 cli->transport->socket->event.ctx);
913 if (net_pipe == NULL) {
914 d_printf("dcerpc_pipe_init failed\n");
918 status = dcerpc_pipe_open_smb(net_pipe->conn, cli->tree, "\\netlogon");
919 if (!NT_STATUS_IS_OK(status)) {
920 d_printf("dcerpc_pipe_open_smb failed: %s\n",
926 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
927 DCERPC_DEBUG_PRINT_OUT;
930 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
931 status = dcerpc_bind_auth(net_pipe, &dcerpc_table_netlogon,
932 wks_creds, DCERPC_AUTH_TYPE_SCHANNEL,
933 DCERPC_AUTH_LEVEL_PRIVACY,
936 status = dcerpc_bind_auth_none(net_pipe, &dcerpc_table_netlogon);
938 if (!NT_STATUS_IS_OK(status)) {
939 d_printf("schannel bind failed: %s\n", nt_errstr(status));
944 for (i=2; i<4; i++) {
946 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
947 struct creds_CredentialState *creds_state;
948 struct netr_Authenticator netr_auth, netr_auth2;
949 struct netr_NetworkInfo ninfo;
950 struct netr_PasswordInfo pinfo;
951 struct netr_LogonSamLogon r;
953 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
954 CLI_CRED_NTLMv2_AUTH;
956 chal = data_blob_talloc(mem_ctx, NULL, 8);
957 if (chal.data == NULL) {
958 d_printf("data_blob_talloc failed\n");
962 generate_random_buffer(chal.data, chal.length);
963 names_blob = NTLMv2_generate_names_blob(
964 mem_ctx, cli_credentials_get_workstation(user_creds),
965 cli_credentials_get_domain(user_creds));
966 status = cli_credentials_get_ntlm_response(
967 user_creds, mem_ctx, &flags, chal, names_blob,
968 &lm_resp, &nt_resp, NULL, NULL);
969 if (!NT_STATUS_IS_OK(status)) {
970 d_printf("cli_credentials_get_ntlm_response failed:"
971 " %s\n", nt_errstr(status));
975 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
976 creds_client_authenticator(creds_state, &netr_auth);
978 ninfo.identity_info.account_name.string =
979 cli_credentials_get_username(user_creds);
980 ninfo.identity_info.domain_name.string =
981 cli_credentials_get_domain(user_creds);
982 ninfo.identity_info.parameter_control = 0;
983 ninfo.identity_info.logon_id_low = 0;
984 ninfo.identity_info.logon_id_high = 0;
985 ninfo.identity_info.workstation.string =
986 cli_credentials_get_workstation(user_creds);
987 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
988 ninfo.nt.length = nt_resp.length;
989 ninfo.nt.data = nt_resp.data;
990 ninfo.lm.length = lm_resp.length;
991 ninfo.lm.data = lm_resp.data;
993 r.in.server_name = talloc_asprintf(
994 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
995 ZERO_STRUCT(netr_auth2);
997 cli_credentials_get_workstation(wks_creds);
998 r.in.credential = &netr_auth;
999 r.in.return_authenticator = &netr_auth2;
1000 r.in.logon_level = 2;
1001 r.in.validation_level = i;
1002 r.in.logon.network = &ninfo;
1003 r.out.return_authenticator = NULL;
1005 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1006 if (!NT_STATUS_IS_OK(status)) {
1007 d_printf("netr_LogonSamLogon failed: %s\n",
1012 if ((r.out.return_authenticator == NULL) ||
1013 (!creds_client_check(creds_state,
1014 &r.out.return_authenticator->cred))) {
1015 d_printf("Credentials check failed!\n");
1019 creds_client_authenticator(creds_state, &netr_auth);
1021 pinfo.identity_info = ninfo.identity_info;
1022 ZERO_STRUCT(pinfo.lmpassword.hash);
1023 E_md4hash(cli_credentials_get_password(user_creds),
1024 pinfo.ntpassword.hash);
1025 session_key = data_blob_talloc(mem_ctx,
1026 creds_state->session_key, 16);
1027 arcfour_crypt_blob(pinfo.ntpassword.hash,
1028 sizeof(pinfo.ntpassword.hash),
1031 r.in.logon_level = 1;
1032 r.in.logon.password = &pinfo;
1033 r.out.return_authenticator = NULL;
1035 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1036 if (!NT_STATUS_IS_OK(status)) {
1037 d_printf("netr_LogonSamLogon failed: %s\n",
1042 if ((r.out.return_authenticator == NULL) ||
1043 (!creds_client_check(creds_state,
1044 &r.out.return_authenticator->cred))) {
1045 d_printf("Credentials check failed!\n");
1051 struct netr_ServerPasswordSet s;
1052 char *password = generate_random_str(wks_creds, 8);
1053 struct creds_CredentialState *creds_state;
1055 s.in.server_name = talloc_asprintf(
1056 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1057 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1058 s.in.account_name = talloc_asprintf(
1059 mem_ctx, "%s$", s.in.computer_name);
1060 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1061 E_md4hash(password, s.in.new_password.hash);
1063 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1064 creds_des_encrypt(creds_state, &s.in.new_password);
1065 creds_client_authenticator(creds_state, &s.in.credential);
1067 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1068 if (!NT_STATUS_IS_OK(status)) {
1069 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1073 if (!creds_client_check(creds_state,
1074 &s.out.return_authenticator.cred)) {
1075 printf("Credential chaining failed\n");
1078 cli_credentials_set_password(wks_creds, password,
1084 talloc_free(mem_ctx);
1089 * Delete the wks account again
1092 static BOOL leave(struct smbcli_state *cli,
1093 struct cli_credentials *admin_creds,
1094 struct cli_credentials *wks_creds)
1096 char *wks_name = talloc_asprintf(
1097 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1100 ret = delete_user(cli, admin_creds, wks_name);
1101 talloc_free(wks_name);
1106 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1109 BOOL torture_netlogon_samba3(struct torture_context *torture)
1111 TALLOC_CTX *mem_ctx;
1114 struct smbcli_state *cli;
1115 struct cli_credentials *anon_creds;
1116 struct cli_credentials *wks_creds;
1117 const char *wks_name;
1120 wks_name = lp_parm_string(-1, "torture", "wksname");
1121 if (wks_name == NULL) {
1122 wks_name = get_myname();
1125 mem_ctx = talloc_init("torture_bind_authcontext");
1127 if (mem_ctx == NULL) {
1128 d_printf("talloc_init failed\n");
1132 if (!(anon_creds = create_anon_creds(mem_ctx))) {
1133 d_printf("create_anon_creds failed\n");
1137 status = smbcli_full_connection(mem_ctx, &cli,
1138 lp_parm_string(-1, "torture", "host"),
1139 "IPC$", NULL, anon_creds, NULL);
1140 if (!NT_STATUS_IS_OK(status)) {
1141 d_printf("smbcli_full_connection failed: %s\n",
1146 wks_creds = cli_credentials_init(mem_ctx);
1147 if (wks_creds == NULL) {
1148 d_printf("cli_credentials_init failed\n");
1152 cli_credentials_set_conf(wks_creds);
1153 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1154 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1155 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1156 cli_credentials_set_password(wks_creds,
1157 generate_random_str(wks_creds, 8),
1160 if (!join3(cli, False, cmdline_credentials, wks_creds)) {
1161 d_printf("join failed\n");
1165 cli_credentials_set_domain(
1166 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1169 for (i=0; i<2; i++) {
1171 /* Do this more than once, the routine "schan" changes
1172 * the workstation password using the netlogon
1173 * password change routine */
1177 if (!auth2(cli, wks_creds)) {
1178 d_printf("auth2 failed\n");
1182 for (j=0; j<2; j++) {
1183 if (!schan(cli, wks_creds, cmdline_credentials)) {
1184 d_printf("schan failed\n");
1190 if (!leave(cli, cmdline_credentials, wks_creds)) {
1191 d_printf("leave failed\n");
1198 talloc_free(mem_ctx);
1203 * Do a simple join, testjoin and leave using specified smb and samr
1207 static BOOL test_join3(TALLOC_CTX *mem_ctx,
1209 struct cli_credentials *smb_creds,
1210 struct cli_credentials *samr_creds,
1211 const char *wks_name)
1215 struct smbcli_state *cli;
1216 struct cli_credentials *wks_creds;
1218 status = smbcli_full_connection(mem_ctx, &cli,
1219 lp_parm_string(-1, "torture", "host"),
1220 "IPC$", NULL, smb_creds, NULL);
1221 if (!NT_STATUS_IS_OK(status)) {
1222 d_printf("smbcli_full_connection failed: %s\n",
1227 wks_creds = cli_credentials_init(cli);
1228 if (wks_creds == NULL) {
1229 d_printf("cli_credentials_init failed\n");
1233 cli_credentials_set_conf(wks_creds);
1234 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1235 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1236 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1237 cli_credentials_set_password(wks_creds,
1238 generate_random_str(wks_creds, 8),
1241 if (!join3(cli, use_level25, samr_creds, wks_creds)) {
1242 d_printf("join failed\n");
1246 cli_credentials_set_domain(
1247 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1250 if (!auth2(cli, wks_creds)) {
1251 d_printf("auth2 failed\n");
1255 if (!leave(cli, samr_creds, wks_creds)) {
1256 d_printf("leave failed\n");
1269 * Test the different session key variants. Do it by joining, this uses the
1270 * session key in the setpassword routine. Test the join by doing the auth2.
1273 BOOL torture_samba3_sessionkey(struct torture_context *torture)
1275 TALLOC_CTX *mem_ctx;
1277 struct cli_credentials *anon_creds;
1278 const char *wks_name;
1280 wks_name = lp_parm_string(-1, "torture", "wksname");
1281 if (wks_name == NULL) {
1282 wks_name = get_myname();
1285 mem_ctx = talloc_init("torture_samba3_sessionkey");
1287 if (mem_ctx == NULL) {
1288 d_printf("talloc_init failed\n");
1292 if (!(anon_creds = create_anon_creds(mem_ctx))) {
1293 d_printf("create_anon_creds failed\n");
1299 if (!lp_parm_bool(-1, "target", "samba3", False)) {
1301 /* Samba3 in the build farm right now does this happily. Need
1304 if (test_join3(mem_ctx, False, anon_creds, NULL, wks_name)) {
1305 d_printf("join using anonymous bind on an anonymous smb "
1306 "connection succeeded -- HUH??\n");
1311 if (!test_join3(mem_ctx, False, anon_creds, cmdline_credentials,
1313 d_printf("join using ntlmssp bind on an anonymous smb "
1314 "connection failed\n");
1318 if (!test_join3(mem_ctx, False, cmdline_credentials, NULL, wks_name)) {
1319 d_printf("join using anonymous bind on an authenticated smb "
1320 "connection failed\n");
1324 if (!test_join3(mem_ctx, False, cmdline_credentials,
1325 cmdline_credentials,
1327 d_printf("join using ntlmssp bind on an authenticated smb "
1328 "connection failed\n");
1333 * The following two are tests for setuserinfolevel 25
1336 if (!test_join3(mem_ctx, True, anon_creds, cmdline_credentials,
1338 d_printf("join using ntlmssp bind on an anonymous smb "
1339 "connection failed\n");
1343 if (!test_join3(mem_ctx, True, cmdline_credentials, NULL, wks_name)) {
1344 d_printf("join using anonymous bind on an authenticated smb "
1345 "connection failed\n");
1355 * open pipe and bind, given an IPC$ context
1358 static struct dcerpc_pipe *pipe_bind_smb(TALLOC_CTX *mem_ctx,
1359 struct smbcli_tree *tree,
1360 const char *pipe_name,
1361 const struct dcerpc_interface_table *iface)
1363 struct dcerpc_pipe *result;
1366 if (!(result = dcerpc_pipe_init(
1367 mem_ctx, tree->session->transport->socket->event.ctx))) {
1371 status = dcerpc_pipe_open_smb(result->conn, tree, pipe_name);
1372 if (!NT_STATUS_IS_OK(status)) {
1373 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1375 talloc_free(result);
1379 status = dcerpc_bind_auth_none(result, iface);
1380 if (!NT_STATUS_IS_OK(status)) {
1381 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1382 talloc_free(result);
1390 * Sane wrapper around lsa_LookupNames
1393 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1394 struct dcerpc_pipe *p,
1398 struct lsa_ObjectAttribute attr;
1399 struct lsa_QosInfo qos;
1400 struct lsa_OpenPolicy2 r;
1403 struct policy_handle handle;
1404 struct lsa_LookupNames l;
1405 struct lsa_TransSidArray sids;
1406 struct lsa_String lsa_name;
1408 struct dom_sid *result;
1409 TALLOC_CTX *tmp_ctx;
1411 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1416 qos.impersonation_level = 2;
1417 qos.context_mode = 1;
1418 qos.effective_only = 0;
1421 attr.root_dir = NULL;
1422 attr.object_name = NULL;
1423 attr.attributes = 0;
1424 attr.sec_desc = NULL;
1425 attr.sec_qos = &qos;
1427 r.in.system_name = "\\";
1429 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1430 r.out.handle = &handle;
1432 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1433 if (!NT_STATUS_IS_OK(status)) {
1434 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1435 talloc_free(tmp_ctx);
1442 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1444 l.in.handle = &handle;
1446 l.in.names = &lsa_name;
1449 l.in.count = &count;
1450 l.out.count = &count;
1453 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1454 if (!NT_STATUS_IS_OK(status)) {
1455 printf("LookupNames failed - %s\n", nt_errstr(status));
1456 talloc_free(tmp_ctx);
1460 result = dom_sid_add_rid(mem_ctx, l.out.domains->domains[0].sid,
1461 l.out.sids->sids[0].rid);
1463 c.in.handle = &handle;
1464 c.out.handle = &handle;
1466 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1467 if (!NT_STATUS_IS_OK(status)) {
1468 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1469 talloc_free(tmp_ctx);
1473 talloc_free(tmp_ctx);
1478 * Find out the user SID on this connection
1481 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree)
1483 struct dcerpc_pipe *lsa;
1484 struct lsa_GetUserName r;
1486 struct lsa_StringPointer authority_name_p;
1487 struct dom_sid *result;
1489 if (!(lsa = pipe_bind_smb(mem_ctx, tree, "\\pipe\\lsarpc",
1490 &dcerpc_table_lsarpc))) {
1491 d_printf("Could not bind to LSA\n");
1495 r.in.system_name = "\\";
1496 r.in.account_name = NULL;
1497 authority_name_p.string = NULL;
1498 r.in.authority_name = &authority_name_p;
1500 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1502 if (!NT_STATUS_IS_OK(status)) {
1503 printf("GetUserName failed - %s\n", nt_errstr(status));
1508 result = name2sid(mem_ctx, lsa, r.out.account_name->string,
1509 r.out.authority_name->string->string);
1516 * Do a tcon, given a session
1519 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1520 struct smbcli_session *session,
1521 const char *sharename,
1522 struct smbcli_tree **res)
1524 struct smbcli_tree *result;
1525 TALLOC_CTX *tmp_ctx;
1526 union smb_tcon tcon;
1529 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1530 return NT_STATUS_NO_MEMORY;
1533 if (!(result = smbcli_tree_init(session, mem_ctx, False))) {
1534 talloc_free(tmp_ctx);
1535 return NT_STATUS_NO_MEMORY;
1538 tcon.generic.level = RAW_TCON_TCONX;
1539 tcon.tconx.in.flags = 0;
1540 tcon.tconx.in.password = data_blob(NULL, 0);
1541 tcon.tconx.in.path = sharename;
1542 tcon.tconx.in.device = "?????";
1544 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1545 if (!NT_STATUS_IS_OK(status)) {
1546 d_printf("smb_raw_tcon failed: %s\n", nt_errstr(status));
1547 talloc_free(tmp_ctx);
1551 result->tid = tcon.tconx.out.tid;
1552 result = talloc_steal(mem_ctx, result);
1553 talloc_free(tmp_ctx);
1555 return NT_STATUS_OK;
1559 * Test the getusername behaviour
1562 BOOL torture_samba3_rpc_getusername(struct torture_context *torture)
1565 struct smbcli_state *cli;
1566 TALLOC_CTX *mem_ctx;
1568 struct dom_sid *user_sid;
1569 struct dom_sid *created_sid;
1570 struct cli_credentials *anon_creds;
1571 struct cli_credentials *user_creds;
1574 if (!(mem_ctx = talloc_new(torture))) {
1578 status = smbcli_full_connection(
1579 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1580 "IPC$", NULL, cmdline_credentials, NULL);
1581 if (!NT_STATUS_IS_OK(status)) {
1582 d_printf("smbcli_full_connection failed: %s\n",
1588 if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1589 d_printf("whoami on auth'ed connection failed\n");
1595 if (!(anon_creds = create_anon_creds(mem_ctx))) {
1596 d_printf("create_anon_creds failed\n");
1601 status = smbcli_full_connection(
1602 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1603 "IPC$", NULL, anon_creds, NULL);
1604 if (!NT_STATUS_IS_OK(status)) {
1605 d_printf("anon smbcli_full_connection failed: %s\n",
1611 if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1612 d_printf("whoami on anon connection failed\n");
1617 if (!dom_sid_equal(user_sid,
1618 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1619 d_printf("Anon lsa_GetUserName returned %s, expected S-1-5-7",
1620 dom_sid_string(mem_ctx, user_sid));
1624 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1625 d_printf("cli_credentials_init failed\n");
1630 cli_credentials_set_conf(user_creds);
1631 cli_credentials_set_username(user_creds, "torture_username",
1633 cli_credentials_set_password(user_creds,
1634 generate_random_str(user_creds, 8),
1637 if (!create_user(mem_ctx, cli, cmdline_credentials,
1638 cli_credentials_get_username(user_creds),
1639 cli_credentials_get_password(user_creds),
1640 &domain_name, &created_sid)) {
1641 d_printf("create_user failed\n");
1646 cli_credentials_set_domain(user_creds, domain_name,
1650 struct smbcli_session *session2;
1651 struct smb_composite_sesssetup setup;
1652 struct smbcli_tree *tree;
1654 session2 = smbcli_session_init(cli->transport, mem_ctx, False);
1655 if (session2 == NULL) {
1656 d_printf("smbcli_session_init failed\n");
1660 setup.in.sesskey = cli->transport->negotiate.sesskey;
1661 setup.in.capabilities = cli->transport->negotiate.capabilities;
1662 setup.in.workgroup = "";
1663 setup.in.credentials = user_creds;
1665 status = smb_composite_sesssetup(session2, &setup);
1666 if (!NT_STATUS_IS_OK(status)) {
1667 d_printf("anon session setup failed: %s\n",
1673 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1675 d_printf("secondary_tcon failed\n");
1680 if (!(user_sid = whoami(mem_ctx, tree))) {
1681 d_printf("whoami on user connection failed\n");
1689 d_printf("Created %s, found %s\n",
1690 dom_sid_string(mem_ctx, created_sid),
1691 dom_sid_string(mem_ctx, user_sid));
1693 if (!dom_sid_equal(created_sid, user_sid)) {
1698 if (!delete_user(cli, cmdline_credentials,
1699 cli_credentials_get_username(user_creds))) {
1700 d_printf("delete_user failed\n");
1705 talloc_free(mem_ctx);
1709 static BOOL test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1710 const char *sharename)
1713 struct srvsvc_NetShareGetInfo r;
1714 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1718 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1719 dcerpc_server_name(p));
1720 r.in.share_name = sharename;
1722 for (i=0;i<ARRAY_SIZE(levels);i++) {
1723 r.in.level = levels[i];
1727 printf("testing NetShareGetInfo level %u on share '%s'\n",
1728 r.in.level, r.in.share_name);
1730 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1731 if (!NT_STATUS_IS_OK(status)) {
1732 printf("NetShareGetInfo level %u on share '%s' failed"
1733 " - %s\n", r.in.level, r.in.share_name,
1738 if (!W_ERROR_IS_OK(r.out.result)) {
1739 printf("NetShareGetInfo level %u on share '%s' failed "
1740 "- %s\n", r.in.level, r.in.share_name,
1741 win_errstr(r.out.result));
1750 static BOOL test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1751 const char **one_sharename)
1754 struct srvsvc_NetShareEnum r;
1755 struct srvsvc_NetShareCtr0 c0;
1756 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1760 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1761 r.in.ctr.ctr0 = &c0;
1762 r.in.ctr.ctr0->count = 0;
1763 r.in.ctr.ctr0->array = NULL;
1764 r.in.max_buffer = (uint32_t)-1;
1765 r.in.resume_handle = NULL;
1767 for (i=0;i<ARRAY_SIZE(levels);i++) {
1768 r.in.level = levels[i];
1772 printf("testing NetShareEnum level %u\n", r.in.level);
1773 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1774 if (!NT_STATUS_IS_OK(status)) {
1775 printf("NetShareEnum level %u failed - %s\n",
1776 r.in.level, nt_errstr(status));
1780 if (!W_ERROR_IS_OK(r.out.result)) {
1781 printf("NetShareEnum level %u failed - %s\n",
1782 r.in.level, win_errstr(r.out.result));
1785 if (r.in.level == 0) {
1786 struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1787 if (ctr->count > 0) {
1788 *one_sharename = ctr->array[0].name;
1796 BOOL torture_samba3_rpc_srvsvc(struct torture_context *torture)
1798 struct dcerpc_pipe *p;
1799 TALLOC_CTX *mem_ctx;
1801 const char *sharename = NULL;
1802 struct smbcli_state *cli;
1804 if (!(mem_ctx = talloc_new(torture))) {
1808 if (!(torture_open_connection_share(
1809 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1811 talloc_free(mem_ctx);
1815 if (!(p = pipe_bind_smb(mem_ctx, cli->tree, "\\pipe\\srvsvc",
1816 &dcerpc_table_srvsvc))) {
1817 d_printf("could not bind to srvsvc pipe\n");
1822 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1823 if (sharename == NULL) {
1824 printf("did not get sharename\n");
1826 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1830 talloc_free(mem_ctx);
1834 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
1835 struct smbcli_session *sess,
1836 const char *sharename)
1838 struct smbcli_tree *tree;
1839 TALLOC_CTX *tmp_ctx;
1840 struct dcerpc_pipe *p;
1842 struct srvsvc_NetShareGetInfo r;
1843 struct security_descriptor *result;
1845 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1846 d_printf("talloc_new failed\n");
1850 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
1851 d_printf("secondary_tcon failed\n");
1852 talloc_free(tmp_ctx);
1856 if (!(p = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
1857 &dcerpc_table_srvsvc))) {
1858 d_printf("could not bind to srvsvc pipe\n");
1859 talloc_free(tmp_ctx);
1864 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
1867 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
1868 dcerpc_server_name(p));
1869 r.in.share_name = sharename;
1872 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
1873 if (!NT_STATUS_IS_OK(status)) {
1874 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
1876 talloc_free(tmp_ctx);
1880 result = talloc_steal(mem_ctx, r.out.info.info502->sd);
1881 talloc_free(tmp_ctx);
1885 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
1886 struct smbcli_session *sess,
1887 const char *sharename,
1888 struct security_descriptor *sd)
1890 struct smbcli_tree *tree;
1891 TALLOC_CTX *tmp_ctx;
1892 struct dcerpc_pipe *p;
1894 struct sec_desc_buf i;
1895 struct srvsvc_NetShareSetInfo r;
1898 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1899 d_printf("talloc_new failed\n");
1900 return NT_STATUS_NO_MEMORY;
1903 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
1904 d_printf("secondary_tcon failed\n");
1905 talloc_free(tmp_ctx);
1906 return NT_STATUS_UNSUCCESSFUL;
1909 if (!(p = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
1910 &dcerpc_table_srvsvc))) {
1911 d_printf("could not bind to srvsvc pipe\n");
1912 talloc_free(tmp_ctx);
1913 return NT_STATUS_UNSUCCESSFUL;
1917 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
1920 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
1921 dcerpc_server_name(p));
1922 r.in.share_name = sharename;
1925 r.in.info.info1501 = &i;
1926 r.in.parm_error = &error;
1928 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
1929 if (!NT_STATUS_IS_OK(status)) {
1930 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
1934 talloc_free(tmp_ctx);
1938 BOOL try_tcon(TALLOC_CTX *mem_ctx,
1939 struct security_descriptor *orig_sd,
1940 struct smbcli_session *session,
1941 const char *sharename, const struct dom_sid *user_sid,
1942 unsigned int access_mask, NTSTATUS expected_tcon,
1943 NTSTATUS expected_mkdir)
1945 TALLOC_CTX *tmp_ctx;
1946 struct smbcli_tree *rmdir_tree, *tree;
1947 struct dom_sid *domain_sid;
1949 struct security_descriptor *sd;
1953 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1954 d_printf("talloc_new failed\n");
1958 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
1959 if (!NT_STATUS_IS_OK(status)) {
1960 d_printf("first tcon to delete dir failed\n");
1961 talloc_free(tmp_ctx);
1965 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
1967 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
1968 &domain_sid, &rid))) {
1969 d_printf("dom_sid_split_rid failed\n");
1970 talloc_free(tmp_ctx);
1974 sd = security_descriptor_create(
1975 tmp_ctx, "S-1-5-32-544",
1976 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
1978 dom_sid_string(mem_ctx, user_sid),
1979 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
1981 d_printf("security_descriptor_create failed\n");
1982 talloc_free(tmp_ctx);
1986 status = set_sharesec(mem_ctx, session, sharename, sd);
1987 if (!NT_STATUS_IS_OK(status)) {
1988 d_printf("custom set_sharesec failed: %s\n",
1990 talloc_free(tmp_ctx);
1994 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
1995 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
1996 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2002 if (!NT_STATUS_IS_OK(status)) {
2003 /* An expected non-access, no point in trying to write */
2007 status = smbcli_mkdir(tree, "sharesec_testdir");
2008 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2009 d_printf("Expected %s, got %s\n", nt_errstr(expected_mkdir),
2015 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2017 status = set_sharesec(mem_ctx, session, sharename, orig_sd);
2018 if (!NT_STATUS_IS_OK(status)) {
2019 d_printf("custom set_sharesec failed: %s\n",
2021 talloc_free(tmp_ctx);
2025 talloc_free(tmp_ctx);
2029 BOOL torture_samba3_rpc_sharesec(struct torture_context *torture)
2031 TALLOC_CTX *mem_ctx;
2033 struct smbcli_state *cli;
2034 struct security_descriptor *sd;
2035 struct dom_sid *user_sid;
2037 if (!(mem_ctx = talloc_new(torture))) {
2041 if (!(torture_open_connection_share(
2042 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
2044 d_printf("IPC$ connection failed\n");
2045 talloc_free(mem_ctx);
2049 if (!(user_sid = whoami(mem_ctx, cli->tree))) {
2050 d_printf("whoami failed\n");
2051 talloc_free(mem_ctx);
2055 sd = get_sharesec(mem_ctx, cli->session, lp_parm_string(-1, "torture",
2058 ret &= try_tcon(mem_ctx, sd, cli->session,
2059 lp_parm_string(-1, "torture", "share"),
2060 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2062 ret &= try_tcon(mem_ctx, sd, cli->session,
2063 lp_parm_string(-1, "torture", "share"),
2064 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2065 NT_STATUS_NETWORK_ACCESS_DENIED);
2067 ret &= try_tcon(mem_ctx, sd, cli->session,
2068 lp_parm_string(-1, "torture", "share"),
2069 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2071 talloc_free(mem_ctx);