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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/rap/rap.h"
25 #include "torture/torture.h"
26 #include "torture/util.h"
27 #include "torture/rap/proto.h"
28 #include "librpc/gen_ndr/ndr_lsa.h"
29 #include "librpc/gen_ndr/ndr_lsa_c.h"
30 #include "librpc/gen_ndr/ndr_samr.h"
31 #include "librpc/gen_ndr/ndr_samr_c.h"
32 #include "librpc/gen_ndr/ndr_netlogon.h"
33 #include "librpc/gen_ndr/ndr_netlogon_c.h"
34 #include "librpc/gen_ndr/ndr_srvsvc.h"
35 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
36 #include "librpc/gen_ndr/ndr_spoolss.h"
37 #include "librpc/gen_ndr/ndr_spoolss_c.h"
38 #include "librpc/gen_ndr/ndr_winreg.h"
39 #include "librpc/gen_ndr/ndr_winreg_c.h"
40 #include "librpc/gen_ndr/ndr_wkssvc.h"
41 #include "librpc/gen_ndr/ndr_wkssvc_c.h"
42 #include "lib/cmdline/popt_common.h"
43 #include "librpc/rpc/dcerpc.h"
44 #include "torture/rpc/rpc.h"
45 #include "libcli/libcli.h"
46 #include "libcli/composite/composite.h"
47 #include "libcli/smb_composite/smb_composite.h"
48 #include "libcli/auth/libcli_auth.h"
49 #include "lib/crypto/crypto.h"
50 #include "libcli/security/proto.h"
51 #include "param/param.h"
53 static struct cli_credentials *create_anon_creds(TALLOC_CTX *mem_ctx)
55 struct cli_credentials *result;
57 if (!(result = cli_credentials_init(mem_ctx))) {
61 cli_credentials_set_conf(result, global_loadparm);
62 cli_credentials_set_anonymous(result);
68 * This tests a RPC call using an invalid vuid
71 bool torture_bind_authcontext(struct torture_context *torture)
76 struct lsa_ObjectAttribute objectattr;
77 struct lsa_OpenPolicy2 openpolicy;
78 struct policy_handle handle;
79 struct lsa_Close close_handle;
80 struct smbcli_session *tmp;
81 struct smbcli_session *session2;
82 struct smbcli_state *cli;
83 struct dcerpc_pipe *lsa_pipe;
84 struct cli_credentials *anon_creds;
85 struct smb_composite_sesssetup setup;
87 mem_ctx = talloc_init("torture_bind_authcontext");
89 if (mem_ctx == NULL) {
90 d_printf("talloc_init failed\n");
94 status = smbcli_full_connection(mem_ctx, &cli,
95 torture_setting_string(torture, "host", NULL),
96 "IPC$", NULL, cmdline_credentials,
98 if (!NT_STATUS_IS_OK(status)) {
99 d_printf("smbcli_full_connection failed: %s\n",
104 lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx);
105 if (lsa_pipe == NULL) {
106 d_printf("dcerpc_pipe_init failed\n");
110 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
111 if (!NT_STATUS_IS_OK(status)) {
112 d_printf("dcerpc_pipe_open_smb failed: %s\n",
117 status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
118 if (!NT_STATUS_IS_OK(status)) {
119 d_printf("dcerpc_bind_auth_none failed: %s\n",
124 openpolicy.in.system_name =talloc_asprintf(
125 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
126 ZERO_STRUCT(objectattr);
127 openpolicy.in.attr = &objectattr;
128 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
129 openpolicy.out.handle = &handle;
131 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
133 if (!NT_STATUS_IS_OK(status)) {
134 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
139 close_handle.in.handle = &handle;
140 close_handle.out.handle = &handle;
142 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
143 if (!NT_STATUS_IS_OK(status)) {
144 d_printf("dcerpc_lsa_Close failed: %s\n",
149 session2 = smbcli_session_init(cli->transport, mem_ctx, false);
150 if (session2 == NULL) {
151 d_printf("smbcli_session_init failed\n");
155 if (!(anon_creds = create_anon_creds(mem_ctx))) {
156 d_printf("create_anon_creds failed\n");
160 setup.in.sesskey = cli->transport->negotiate.sesskey;
161 setup.in.capabilities = cli->transport->negotiate.capabilities;
162 setup.in.workgroup = "";
163 setup.in.credentials = anon_creds;
165 status = smb_composite_sesssetup(session2, &setup);
166 if (!NT_STATUS_IS_OK(status)) {
167 d_printf("anon session setup failed: %s\n",
171 session2->vuid = setup.out.vuid;
173 tmp = cli->tree->session;
174 cli->tree->session = session2;
176 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
178 cli->tree->session = tmp;
179 talloc_free(lsa_pipe);
182 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
183 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
184 "expected NT_STATUS_INVALID_HANDLE\n",
191 talloc_free(mem_ctx);
196 * Bind to lsa using a specific auth method
199 static bool bindtest(struct smbcli_state *cli,
200 struct cli_credentials *credentials,
201 uint8_t auth_type, uint8_t auth_level)
207 struct dcerpc_pipe *lsa_pipe;
208 struct lsa_ObjectAttribute objectattr;
209 struct lsa_OpenPolicy2 openpolicy;
210 struct lsa_QueryInfoPolicy query;
211 struct policy_handle handle;
212 struct lsa_Close close_handle;
214 if ((mem_ctx = talloc_init("bindtest")) == NULL) {
215 d_printf("talloc_init failed\n");
219 lsa_pipe = dcerpc_pipe_init(mem_ctx,
220 cli->transport->socket->event.ctx);
221 if (lsa_pipe == NULL) {
222 d_printf("dcerpc_pipe_init failed\n");
226 status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
227 if (!NT_STATUS_IS_OK(status)) {
228 d_printf("dcerpc_pipe_open_smb failed: %s\n",
233 status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
234 credentials, auth_type, auth_level,
236 if (!NT_STATUS_IS_OK(status)) {
237 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
241 openpolicy.in.system_name =talloc_asprintf(
242 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
243 ZERO_STRUCT(objectattr);
244 openpolicy.in.attr = &objectattr;
245 openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
246 openpolicy.out.handle = &handle;
248 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
250 if (!NT_STATUS_IS_OK(status)) {
251 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
256 query.in.handle = &handle;
257 query.in.level = LSA_POLICY_INFO_DOMAIN;
259 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
260 if (!NT_STATUS_IS_OK(status)) {
261 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
266 close_handle.in.handle = &handle;
267 close_handle.out.handle = &handle;
269 status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
270 if (!NT_STATUS_IS_OK(status)) {
271 d_printf("dcerpc_lsa_Close failed: %s\n",
278 talloc_free(mem_ctx);
283 * test authenticated RPC binds with the variants Samba3 does support
286 bool torture_bind_samba3(struct torture_context *torture)
291 struct smbcli_state *cli;
293 mem_ctx = talloc_init("torture_bind_authcontext");
295 if (mem_ctx == NULL) {
296 d_printf("talloc_init failed\n");
300 status = smbcli_full_connection(mem_ctx, &cli,
301 torture_setting_string(torture, "host", NULL),
302 "IPC$", NULL, cmdline_credentials,
304 if (!NT_STATUS_IS_OK(status)) {
305 d_printf("smbcli_full_connection failed: %s\n",
312 ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
313 DCERPC_AUTH_LEVEL_INTEGRITY);
314 ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_NTLMSSP,
315 DCERPC_AUTH_LEVEL_PRIVACY);
316 ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
317 DCERPC_AUTH_LEVEL_INTEGRITY);
318 ret &= bindtest(cli, cmdline_credentials, DCERPC_AUTH_TYPE_SPNEGO,
319 DCERPC_AUTH_LEVEL_PRIVACY);
322 talloc_free(mem_ctx);
327 * Lookup or create a user and return all necessary info
330 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
332 struct cli_credentials *admin_creds,
335 const char *username,
337 struct dcerpc_pipe **result_pipe,
338 struct policy_handle **result_handle,
339 struct dom_sid **sid)
341 struct dcerpc_pipe *samr_pipe;
343 struct policy_handle conn_handle;
344 struct policy_handle domain_handle;
345 struct policy_handle *user_handle;
346 struct samr_Connect2 conn;
347 struct samr_EnumDomains enumdom;
348 uint32_t resume_handle = 0;
349 struct samr_LookupDomain l;
351 struct lsa_String domain_name;
352 struct lsa_String user_name;
353 struct samr_OpenDomain o;
354 struct samr_CreateUser2 c;
355 uint32_t user_rid,access_granted;
357 samr_pipe = dcerpc_pipe_init(mem_ctx,
358 cli->transport->socket->event.ctx);
359 if (samr_pipe == NULL) {
360 d_printf("dcerpc_pipe_init failed\n");
361 status = NT_STATUS_NO_MEMORY;
365 status = dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr");
366 if (!NT_STATUS_IS_OK(status)) {
367 d_printf("dcerpc_pipe_open_smb failed: %s\n",
372 if (admin_creds != NULL) {
373 status = dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
374 admin_creds, auth_type, auth_level,
376 if (!NT_STATUS_IS_OK(status)) {
377 d_printf("dcerpc_bind_auth failed: %s\n",
382 /* We must have an authenticated SMB connection */
383 status = dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr);
384 if (!NT_STATUS_IS_OK(status)) {
385 d_printf("dcerpc_bind_auth_none failed: %s\n",
391 conn.in.system_name = talloc_asprintf(
392 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
393 conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
394 conn.out.connect_handle = &conn_handle;
396 status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
397 if (!NT_STATUS_IS_OK(status)) {
398 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
402 enumdom.in.connect_handle = &conn_handle;
403 enumdom.in.resume_handle = &resume_handle;
404 enumdom.in.buf_size = (uint32_t)-1;
405 enumdom.out.resume_handle = &resume_handle;
407 status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
408 if (!NT_STATUS_IS_OK(status)) {
409 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
413 if (enumdom.out.num_entries != 2) {
414 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
415 enumdom.out.num_entries);
416 status = NT_STATUS_UNSUCCESSFUL;
420 dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
423 l.in.connect_handle = &conn_handle;
424 domain_name.string = enumdom.out.sam->entries[0].name.string;
425 *domain = talloc_strdup(mem_ctx, domain_name.string);
426 l.in.domain_name = &domain_name;
428 status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
429 if (!NT_STATUS_IS_OK(status)) {
430 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
434 o.in.connect_handle = &conn_handle;
435 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
436 o.in.sid = l.out.sid;
437 o.out.domain_handle = &domain_handle;
439 status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
440 if (!NT_STATUS_IS_OK(status)) {
441 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
445 c.in.domain_handle = &domain_handle;
446 user_name.string = username;
447 c.in.account_name = &user_name;
448 c.in.acct_flags = ACB_NORMAL;
449 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
450 user_handle = talloc(mem_ctx, struct policy_handle);
451 c.out.user_handle = user_handle;
452 c.out.access_granted = &access_granted;
453 c.out.rid = &user_rid;
455 status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
457 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
458 struct samr_LookupNames ln;
459 struct samr_OpenUser ou;
461 ln.in.domain_handle = &domain_handle;
463 ln.in.names = &user_name;
465 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
466 if (!NT_STATUS_IS_OK(status)) {
467 d_printf("samr_LookupNames failed: %s\n",
472 ou.in.domain_handle = &domain_handle;
473 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
474 user_rid = ou.in.rid = ln.out.rids.ids[0];
475 ou.out.user_handle = user_handle;
477 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
478 if (!NT_STATUS_IS_OK(status)) {
479 d_printf("samr_OpenUser failed: %s\n",
485 if (!NT_STATUS_IS_OK(status)) {
486 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
490 *result_pipe = samr_pipe;
491 *result_handle = user_handle;
493 *sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);
505 static bool create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
506 struct cli_credentials *admin_creds,
507 const char *username, const char *password,
509 struct dom_sid **user_sid)
513 struct dcerpc_pipe *samr_pipe;
514 struct policy_handle *wks_handle;
517 if (!(tmp_ctx = talloc_new(mem_ctx))) {
518 d_printf("talloc_init failed\n");
522 status = get_usr_handle(cli, tmp_ctx, admin_creds,
523 DCERPC_AUTH_TYPE_NTLMSSP,
524 DCERPC_AUTH_LEVEL_INTEGRITY,
525 username, domain_name, &samr_pipe, &wks_handle,
527 if (!NT_STATUS_IS_OK(status)) {
528 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
533 struct samr_SetUserInfo2 sui2;
534 struct samr_SetUserInfo sui;
535 struct samr_QueryUserInfo qui;
536 union samr_UserInfo u_info;
537 DATA_BLOB session_key;
541 encode_pw_buffer(u_info.info23.password.data, password,
544 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
545 if (!NT_STATUS_IS_OK(status)) {
546 d_printf("dcerpc_fetch_session_key failed\n");
549 arcfour_crypt_blob(u_info.info23.password.data, 516,
551 u_info.info23.info.password_expired = 0;
552 u_info.info23.info.fields_present = SAMR_FIELD_PASSWORD |
553 SAMR_FIELD_PASSWORD2 |
554 SAMR_FIELD_EXPIRED_FLAG;
555 sui2.in.user_handle = wks_handle;
556 sui2.in.info = &u_info;
559 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
560 if (!NT_STATUS_IS_OK(status)) {
561 d_printf("samr_SetUserInfo(23) failed: %s\n",
566 u_info.info16.acct_flags = ACB_NORMAL;
567 sui.in.user_handle = wks_handle;
568 sui.in.info = &u_info;
571 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
572 if (!NT_STATUS_IS_OK(status)) {
573 d_printf("samr_SetUserInfo(16) failed\n");
577 qui.in.user_handle = wks_handle;
580 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
581 if (!NT_STATUS_IS_OK(status)) {
582 d_printf("samr_QueryUserInfo(21) failed\n");
586 qui.out.info->info21.allow_password_change = 0;
587 qui.out.info->info21.force_password_change = 0;
588 qui.out.info->info21.account_name.string = NULL;
589 qui.out.info->info21.rid = 0;
590 qui.out.info->info21.acct_expiry = 0;
591 qui.out.info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
593 u_info.info21 = qui.out.info->info21;
594 sui.in.user_handle = wks_handle;
595 sui.in.info = &u_info;
598 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
599 if (!NT_STATUS_IS_OK(status)) {
600 d_printf("samr_SetUserInfo(21) failed\n");
605 *domain_name= talloc_steal(mem_ctx, *domain_name);
606 *user_sid = talloc_steal(mem_ctx, *user_sid);
609 talloc_free(tmp_ctx);
617 static bool delete_user(struct smbcli_state *cli,
618 struct cli_credentials *admin_creds,
619 const char *username)
624 struct dcerpc_pipe *samr_pipe;
625 struct policy_handle *user_handle;
628 if ((mem_ctx = talloc_init("leave")) == NULL) {
629 d_printf("talloc_init failed\n");
633 status = get_usr_handle(cli, mem_ctx, admin_creds,
634 DCERPC_AUTH_TYPE_NTLMSSP,
635 DCERPC_AUTH_LEVEL_INTEGRITY,
636 username, &dom_name, &samr_pipe,
639 if (!NT_STATUS_IS_OK(status)) {
640 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
645 struct samr_DeleteUser d;
647 d.in.user_handle = user_handle;
648 d.out.user_handle = user_handle;
650 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
651 if (!NT_STATUS_IS_OK(status)) {
652 d_printf("samr_DeleteUser failed\n");
660 talloc_free(mem_ctx);
665 * Do a Samba3-style join
668 static bool join3(struct smbcli_state *cli,
670 struct cli_credentials *admin_creds,
671 struct cli_credentials *wks_creds)
676 struct dcerpc_pipe *samr_pipe;
677 struct policy_handle *wks_handle;
680 if ((mem_ctx = talloc_init("join3")) == NULL) {
681 d_printf("talloc_init failed\n");
685 status = get_usr_handle(
686 cli, mem_ctx, admin_creds,
687 DCERPC_AUTH_TYPE_NTLMSSP,
688 DCERPC_AUTH_LEVEL_PRIVACY,
689 talloc_asprintf(mem_ctx, "%s$",
690 cli_credentials_get_workstation(wks_creds)),
691 &dom_name, &samr_pipe, &wks_handle, NULL);
693 if (!NT_STATUS_IS_OK(status)) {
694 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
698 cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
701 struct samr_SetUserInfo2 sui2;
702 union samr_UserInfo u_info;
703 struct samr_UserInfo21 *i21 = &u_info.info25.info;
704 DATA_BLOB session_key;
705 DATA_BLOB confounded_session_key = data_blob_talloc(
707 struct MD5Context ctx;
708 uint8_t confounder[16];
712 i21->full_name.string = talloc_asprintf(
714 cli_credentials_get_workstation(wks_creds));
715 i21->acct_flags = ACB_WSTRUST;
716 i21->fields_present = SAMR_FIELD_FULL_NAME |
717 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
719 encode_pw_buffer(u_info.info25.password.data,
720 cli_credentials_get_password(wks_creds),
722 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
723 if (!NT_STATUS_IS_OK(status)) {
724 d_printf("dcerpc_fetch_session_key failed: %s\n",
728 generate_random_buffer((uint8_t *)confounder, 16);
731 MD5Update(&ctx, confounder, 16);
732 MD5Update(&ctx, session_key.data, session_key.length);
733 MD5Final(confounded_session_key.data, &ctx);
735 arcfour_crypt_blob(u_info.info25.password.data, 516,
736 &confounded_session_key);
737 memcpy(&u_info.info25.password.data[516], confounder, 16);
739 sui2.in.user_handle = wks_handle;
741 sui2.in.info = &u_info;
743 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
744 if (!NT_STATUS_IS_OK(status)) {
745 d_printf("samr_SetUserInfo2(25) failed: %s\n",
750 struct samr_SetUserInfo2 sui2;
751 struct samr_SetUserInfo sui;
752 union samr_UserInfo u_info;
753 DATA_BLOB session_key;
755 encode_pw_buffer(u_info.info24.password.data,
756 cli_credentials_get_password(wks_creds),
758 u_info.info24.pw_len =
759 strlen_m(cli_credentials_get_password(wks_creds))*2;
761 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
762 if (!NT_STATUS_IS_OK(status)) {
763 d_printf("dcerpc_fetch_session_key failed\n");
766 arcfour_crypt_blob(u_info.info24.password.data, 516,
768 sui2.in.user_handle = wks_handle;
769 sui2.in.info = &u_info;
772 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
773 if (!NT_STATUS_IS_OK(status)) {
774 d_printf("samr_SetUserInfo(24) failed: %s\n",
779 u_info.info16.acct_flags = ACB_WSTRUST;
780 sui.in.user_handle = wks_handle;
781 sui.in.info = &u_info;
784 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
785 if (!NT_STATUS_IS_OK(status)) {
786 d_printf("samr_SetUserInfo(16) failed\n");
794 talloc_free(mem_ctx);
799 * Do a ReqChallenge/Auth2 and get the wks creds
802 static bool auth2(struct smbcli_state *cli,
803 struct cli_credentials *wks_cred)
806 struct dcerpc_pipe *net_pipe;
809 struct netr_ServerReqChallenge r;
810 struct netr_Credential netr_cli_creds;
811 struct netr_Credential netr_srv_creds;
812 uint32_t negotiate_flags;
813 struct netr_ServerAuthenticate2 a;
814 struct creds_CredentialState *creds_state;
815 struct netr_Credential netr_cred;
816 struct samr_Password mach_pw;
818 mem_ctx = talloc_new(NULL);
819 if (mem_ctx == NULL) {
820 d_printf("talloc_new failed\n");
824 net_pipe = dcerpc_pipe_init(mem_ctx,
825 cli->transport->socket->event.ctx);
826 if (net_pipe == NULL) {
827 d_printf("dcerpc_pipe_init failed\n");
831 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
832 if (!NT_STATUS_IS_OK(status)) {
833 d_printf("dcerpc_pipe_open_smb failed: %s\n",
838 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
839 if (!NT_STATUS_IS_OK(status)) {
840 d_printf("dcerpc_bind_auth_none failed: %s\n",
845 r.in.computer_name = cli_credentials_get_workstation(wks_cred);
846 r.in.server_name = talloc_asprintf(
847 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
848 if (r.in.server_name == NULL) {
849 d_printf("talloc_asprintf failed\n");
852 generate_random_buffer(netr_cli_creds.data,
853 sizeof(netr_cli_creds.data));
854 r.in.credentials = &netr_cli_creds;
855 r.out.credentials = &netr_srv_creds;
857 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
858 if (!NT_STATUS_IS_OK(status)) {
859 d_printf("netr_ServerReqChallenge failed: %s\n",
864 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
865 E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
867 creds_state = talloc(mem_ctx, struct creds_CredentialState);
868 creds_client_init(creds_state, r.in.credentials,
869 r.out.credentials, &mach_pw,
870 &netr_cred, negotiate_flags);
872 a.in.server_name = talloc_asprintf(
873 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
874 a.in.account_name = talloc_asprintf(
875 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
876 a.in.computer_name = cli_credentials_get_workstation(wks_cred);
877 a.in.secure_channel_type = SEC_CHAN_WKSTA;
878 a.in.negotiate_flags = &negotiate_flags;
879 a.out.negotiate_flags = &negotiate_flags;
880 a.in.credentials = &netr_cred;
881 a.out.credentials = &netr_cred;
883 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
884 if (!NT_STATUS_IS_OK(status)) {
885 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
890 if (!creds_client_check(creds_state, a.out.credentials)) {
891 d_printf("creds_client_check failed\n");
895 cli_credentials_set_netlogon_creds(wks_cred, creds_state);
900 talloc_free(mem_ctx);
905 * Do a couple of schannel protected Netlogon ops: Interactive and Network
906 * login, and change the wks password
909 static bool schan(struct smbcli_state *cli,
910 struct cli_credentials *wks_creds,
911 struct cli_credentials *user_creds)
916 struct dcerpc_pipe *net_pipe;
919 mem_ctx = talloc_new(NULL);
920 if (mem_ctx == NULL) {
921 d_printf("talloc_new failed\n");
925 net_pipe = dcerpc_pipe_init(mem_ctx,
926 cli->transport->socket->event.ctx);
927 if (net_pipe == NULL) {
928 d_printf("dcerpc_pipe_init failed\n");
932 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
933 if (!NT_STATUS_IS_OK(status)) {
934 d_printf("dcerpc_pipe_open_smb failed: %s\n",
940 net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
941 DCERPC_DEBUG_PRINT_OUT;
944 net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
945 status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
946 wks_creds, DCERPC_AUTH_TYPE_SCHANNEL,
947 DCERPC_AUTH_LEVEL_PRIVACY,
950 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
952 if (!NT_STATUS_IS_OK(status)) {
953 d_printf("schannel bind failed: %s\n", nt_errstr(status));
958 for (i=2; i<4; i++) {
960 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
961 struct creds_CredentialState *creds_state;
962 struct netr_Authenticator netr_auth, netr_auth2;
963 struct netr_NetworkInfo ninfo;
964 struct netr_PasswordInfo pinfo;
965 struct netr_LogonSamLogon r;
967 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
968 CLI_CRED_NTLMv2_AUTH;
970 chal = data_blob_talloc(mem_ctx, NULL, 8);
971 if (chal.data == NULL) {
972 d_printf("data_blob_talloc failed\n");
976 generate_random_buffer(chal.data, chal.length);
977 names_blob = NTLMv2_generate_names_blob(
978 mem_ctx, cli_credentials_get_workstation(user_creds),
979 cli_credentials_get_domain(user_creds));
980 status = cli_credentials_get_ntlm_response(
981 user_creds, mem_ctx, &flags, chal, names_blob,
982 &lm_resp, &nt_resp, NULL, NULL);
983 if (!NT_STATUS_IS_OK(status)) {
984 d_printf("cli_credentials_get_ntlm_response failed:"
985 " %s\n", nt_errstr(status));
989 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
990 creds_client_authenticator(creds_state, &netr_auth);
992 ninfo.identity_info.account_name.string =
993 cli_credentials_get_username(user_creds);
994 ninfo.identity_info.domain_name.string =
995 cli_credentials_get_domain(user_creds);
996 ninfo.identity_info.parameter_control = 0;
997 ninfo.identity_info.logon_id_low = 0;
998 ninfo.identity_info.logon_id_high = 0;
999 ninfo.identity_info.workstation.string =
1000 cli_credentials_get_workstation(user_creds);
1001 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1002 ninfo.nt.length = nt_resp.length;
1003 ninfo.nt.data = nt_resp.data;
1004 ninfo.lm.length = lm_resp.length;
1005 ninfo.lm.data = lm_resp.data;
1007 r.in.server_name = talloc_asprintf(
1008 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1009 ZERO_STRUCT(netr_auth2);
1010 r.in.computer_name =
1011 cli_credentials_get_workstation(wks_creds);
1012 r.in.credential = &netr_auth;
1013 r.in.return_authenticator = &netr_auth2;
1014 r.in.logon_level = 2;
1015 r.in.validation_level = i;
1016 r.in.logon.network = &ninfo;
1017 r.out.return_authenticator = NULL;
1019 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1020 if (!NT_STATUS_IS_OK(status)) {
1021 d_printf("netr_LogonSamLogon failed: %s\n",
1026 if ((r.out.return_authenticator == NULL) ||
1027 (!creds_client_check(creds_state,
1028 &r.out.return_authenticator->cred))) {
1029 d_printf("Credentials check failed!\n");
1033 creds_client_authenticator(creds_state, &netr_auth);
1035 pinfo.identity_info = ninfo.identity_info;
1036 ZERO_STRUCT(pinfo.lmpassword.hash);
1037 E_md4hash(cli_credentials_get_password(user_creds),
1038 pinfo.ntpassword.hash);
1039 session_key = data_blob_talloc(mem_ctx,
1040 creds_state->session_key, 16);
1041 arcfour_crypt_blob(pinfo.ntpassword.hash,
1042 sizeof(pinfo.ntpassword.hash),
1045 r.in.logon_level = 1;
1046 r.in.logon.password = &pinfo;
1047 r.out.return_authenticator = NULL;
1049 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1050 if (!NT_STATUS_IS_OK(status)) {
1051 d_printf("netr_LogonSamLogon failed: %s\n",
1056 if ((r.out.return_authenticator == NULL) ||
1057 (!creds_client_check(creds_state,
1058 &r.out.return_authenticator->cred))) {
1059 d_printf("Credentials check failed!\n");
1065 struct netr_ServerPasswordSet s;
1066 char *password = generate_random_str(wks_creds, 8);
1067 struct creds_CredentialState *creds_state;
1069 s.in.server_name = talloc_asprintf(
1070 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1071 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1072 s.in.account_name = talloc_asprintf(
1073 mem_ctx, "%s$", s.in.computer_name);
1074 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1075 E_md4hash(password, s.in.new_password.hash);
1077 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1078 creds_des_encrypt(creds_state, &s.in.new_password);
1079 creds_client_authenticator(creds_state, &s.in.credential);
1081 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1082 if (!NT_STATUS_IS_OK(status)) {
1083 printf("ServerPasswordSet - %s\n", nt_errstr(status));
1087 if (!creds_client_check(creds_state,
1088 &s.out.return_authenticator.cred)) {
1089 printf("Credential chaining failed\n");
1092 cli_credentials_set_password(wks_creds, password,
1098 talloc_free(mem_ctx);
1103 * Delete the wks account again
1106 static bool leave(struct smbcli_state *cli,
1107 struct cli_credentials *admin_creds,
1108 struct cli_credentials *wks_creds)
1110 char *wks_name = talloc_asprintf(
1111 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1114 ret = delete_user(cli, admin_creds, wks_name);
1115 talloc_free(wks_name);
1120 * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1123 bool torture_netlogon_samba3(struct torture_context *torture)
1125 TALLOC_CTX *mem_ctx;
1128 struct smbcli_state *cli;
1129 struct cli_credentials *anon_creds;
1130 struct cli_credentials *wks_creds;
1131 const char *wks_name;
1134 wks_name = torture_setting_string(torture, "wksname", NULL);
1135 if (wks_name == NULL) {
1136 wks_name = get_myname();
1139 mem_ctx = talloc_init("torture_netlogon_samba3");
1141 if (mem_ctx == NULL) {
1142 d_printf("talloc_init failed\n");
1146 if (!(anon_creds = create_anon_creds(mem_ctx))) {
1147 d_printf("create_anon_creds failed\n");
1151 status = smbcli_full_connection(mem_ctx, &cli,
1152 torture_setting_string(torture, "host", NULL),
1153 "IPC$", NULL, anon_creds, NULL);
1154 if (!NT_STATUS_IS_OK(status)) {
1155 d_printf("smbcli_full_connection failed: %s\n",
1160 wks_creds = cli_credentials_init(mem_ctx);
1161 if (wks_creds == NULL) {
1162 d_printf("cli_credentials_init failed\n");
1166 cli_credentials_set_conf(wks_creds, global_loadparm);
1167 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1168 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1169 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1170 cli_credentials_set_password(wks_creds,
1171 generate_random_str(wks_creds, 8),
1174 if (!join3(cli, false, cmdline_credentials, wks_creds)) {
1175 d_printf("join failed\n");
1179 cli_credentials_set_domain(
1180 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1183 for (i=0; i<2; i++) {
1185 /* Do this more than once, the routine "schan" changes
1186 * the workstation password using the netlogon
1187 * password change routine */
1191 if (!auth2(cli, wks_creds)) {
1192 d_printf("auth2 failed\n");
1196 for (j=0; j<2; j++) {
1197 if (!schan(cli, wks_creds, cmdline_credentials)) {
1198 d_printf("schan failed\n");
1204 if (!leave(cli, cmdline_credentials, wks_creds)) {
1205 d_printf("leave failed\n");
1212 talloc_free(mem_ctx);
1217 * Do a simple join, testjoin and leave using specified smb and samr
1221 static bool test_join3(TALLOC_CTX *mem_ctx,
1223 struct cli_credentials *smb_creds,
1224 struct cli_credentials *samr_creds,
1225 const char *wks_name)
1229 struct smbcli_state *cli;
1230 struct cli_credentials *wks_creds;
1232 status = smbcli_full_connection(mem_ctx, &cli,
1233 lp_parm_string(global_loadparm, NULL, "torture", "host"),
1234 "IPC$", NULL, smb_creds, NULL);
1235 if (!NT_STATUS_IS_OK(status)) {
1236 d_printf("smbcli_full_connection failed: %s\n",
1241 wks_creds = cli_credentials_init(cli);
1242 if (wks_creds == NULL) {
1243 d_printf("cli_credentials_init failed\n");
1247 cli_credentials_set_conf(wks_creds, global_loadparm);
1248 cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1249 cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1250 cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1251 cli_credentials_set_password(wks_creds,
1252 generate_random_str(wks_creds, 8),
1255 if (!join3(cli, use_level25, samr_creds, wks_creds)) {
1256 d_printf("join failed\n");
1260 cli_credentials_set_domain(
1261 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1264 if (!auth2(cli, wks_creds)) {
1265 d_printf("auth2 failed\n");
1269 if (!leave(cli, samr_creds, wks_creds)) {
1270 d_printf("leave failed\n");
1283 * Test the different session key variants. Do it by joining, this uses the
1284 * session key in the setpassword routine. Test the join by doing the auth2.
1287 bool torture_samba3_sessionkey(struct torture_context *torture)
1289 TALLOC_CTX *mem_ctx;
1291 struct cli_credentials *anon_creds;
1292 const char *wks_name;
1294 wks_name = torture_setting_string(torture, "wksname", get_myname());
1296 mem_ctx = talloc_init("torture_samba3_sessionkey");
1298 if (mem_ctx == NULL) {
1299 d_printf("talloc_init failed\n");
1303 if (!(anon_creds = create_anon_creds(mem_ctx))) {
1304 d_printf("create_anon_creds failed\n");
1310 if (!torture_setting_bool(torture, "samba3", false)) {
1312 /* Samba3 in the build farm right now does this happily. Need
1315 if (test_join3(mem_ctx, false, anon_creds, NULL, wks_name)) {
1316 d_printf("join using anonymous bind on an anonymous smb "
1317 "connection succeeded -- HUH??\n");
1322 if (!test_join3(mem_ctx, false, anon_creds, cmdline_credentials,
1324 d_printf("join using ntlmssp bind on an anonymous smb "
1325 "connection failed\n");
1329 if (!test_join3(mem_ctx, false, cmdline_credentials, NULL, wks_name)) {
1330 d_printf("join using anonymous bind on an authenticated smb "
1331 "connection failed\n");
1335 if (!test_join3(mem_ctx, false, cmdline_credentials,
1336 cmdline_credentials,
1338 d_printf("join using ntlmssp bind on an authenticated smb "
1339 "connection failed\n");
1344 * The following two are tests for setuserinfolevel 25
1347 if (!test_join3(mem_ctx, true, anon_creds, cmdline_credentials,
1349 d_printf("join using ntlmssp bind on an anonymous smb "
1350 "connection failed\n");
1354 if (!test_join3(mem_ctx, true, cmdline_credentials, NULL, wks_name)) {
1355 d_printf("join using anonymous bind on an authenticated smb "
1356 "connection failed\n");
1366 * open pipe and bind, given an IPC$ context
1369 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1370 struct smbcli_tree *tree,
1371 const char *pipe_name,
1372 const struct ndr_interface_table *iface,
1373 struct dcerpc_pipe **p)
1375 struct dcerpc_pipe *result;
1378 if (!(result = dcerpc_pipe_init(
1379 mem_ctx, tree->session->transport->socket->event.ctx))) {
1380 return NT_STATUS_NO_MEMORY;
1383 status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1384 if (!NT_STATUS_IS_OK(status)) {
1385 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1387 talloc_free(result);
1391 status = dcerpc_bind_auth_none(result, iface);
1392 if (!NT_STATUS_IS_OK(status)) {
1393 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1394 talloc_free(result);
1399 return NT_STATUS_OK;
1403 * Sane wrapper around lsa_LookupNames
1406 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1407 struct dcerpc_pipe *p,
1411 struct lsa_ObjectAttribute attr;
1412 struct lsa_QosInfo qos;
1413 struct lsa_OpenPolicy2 r;
1416 struct policy_handle handle;
1417 struct lsa_LookupNames l;
1418 struct lsa_TransSidArray sids;
1419 struct lsa_String lsa_name;
1421 struct dom_sid *result;
1422 TALLOC_CTX *tmp_ctx;
1424 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1429 qos.impersonation_level = 2;
1430 qos.context_mode = 1;
1431 qos.effective_only = 0;
1434 attr.root_dir = NULL;
1435 attr.object_name = NULL;
1436 attr.attributes = 0;
1437 attr.sec_desc = NULL;
1438 attr.sec_qos = &qos;
1440 r.in.system_name = "\\";
1442 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1443 r.out.handle = &handle;
1445 status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1446 if (!NT_STATUS_IS_OK(status)) {
1447 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1448 talloc_free(tmp_ctx);
1455 lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1457 l.in.handle = &handle;
1459 l.in.names = &lsa_name;
1462 l.in.count = &count;
1463 l.out.count = &count;
1466 status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1467 if (!NT_STATUS_IS_OK(status)) {
1468 printf("LookupNames of %s failed - %s\n", lsa_name.string,
1470 talloc_free(tmp_ctx);
1474 result = dom_sid_add_rid(mem_ctx, l.out.domains->domains[0].sid,
1475 l.out.sids->sids[0].rid);
1477 c.in.handle = &handle;
1478 c.out.handle = &handle;
1480 status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1481 if (!NT_STATUS_IS_OK(status)) {
1482 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1483 talloc_free(tmp_ctx);
1487 talloc_free(tmp_ctx);
1492 * Find out the user SID on this connection
1495 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree)
1497 struct dcerpc_pipe *lsa;
1498 struct lsa_GetUserName r;
1500 struct lsa_StringPointer authority_name_p;
1501 struct dom_sid *result;
1503 status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\lsarpc",
1504 &ndr_table_lsarpc, &lsa);
1505 if (!NT_STATUS_IS_OK(status)) {
1506 d_printf("(%s) Could not bind to LSA: %s\n",
1507 __location__, nt_errstr(status));
1511 r.in.system_name = "\\";
1512 r.in.account_name = NULL;
1513 authority_name_p.string = NULL;
1514 r.in.authority_name = &authority_name_p;
1516 status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1518 if (!NT_STATUS_IS_OK(status)) {
1519 printf("(%s) GetUserName failed - %s\n",
1520 __location__, nt_errstr(status));
1525 result = name2sid(mem_ctx, lsa, r.out.account_name->string,
1526 r.out.authority_name->string->string);
1533 * Do a tcon, given a session
1536 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1537 struct smbcli_session *session,
1538 const char *sharename,
1539 struct smbcli_tree **res)
1541 struct smbcli_tree *result;
1542 TALLOC_CTX *tmp_ctx;
1543 union smb_tcon tcon;
1546 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1547 return NT_STATUS_NO_MEMORY;
1550 if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1551 talloc_free(tmp_ctx);
1552 return NT_STATUS_NO_MEMORY;
1555 tcon.generic.level = RAW_TCON_TCONX;
1556 tcon.tconx.in.flags = 0;
1557 tcon.tconx.in.password = data_blob(NULL, 0);
1558 tcon.tconx.in.path = sharename;
1559 tcon.tconx.in.device = "?????";
1561 status = smb_raw_tcon(result, tmp_ctx, &tcon);
1562 if (!NT_STATUS_IS_OK(status)) {
1563 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1565 talloc_free(tmp_ctx);
1569 result->tid = tcon.tconx.out.tid;
1570 result = talloc_steal(mem_ctx, result);
1571 talloc_free(tmp_ctx);
1573 return NT_STATUS_OK;
1577 * Test the getusername behaviour
1580 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1583 struct smbcli_state *cli;
1584 TALLOC_CTX *mem_ctx;
1586 struct dom_sid *user_sid;
1587 struct dom_sid *created_sid;
1588 struct cli_credentials *anon_creds;
1589 struct cli_credentials *user_creds;
1592 if (!(mem_ctx = talloc_new(torture))) {
1596 status = smbcli_full_connection(
1597 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1598 "IPC$", NULL, cmdline_credentials, NULL);
1599 if (!NT_STATUS_IS_OK(status)) {
1600 d_printf("(%s) smbcli_full_connection failed: %s\n",
1601 __location__, nt_errstr(status));
1606 if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1607 d_printf("(%s) whoami on auth'ed connection failed\n",
1614 if (!(anon_creds = create_anon_creds(mem_ctx))) {
1615 d_printf("(%s) create_anon_creds failed\n", __location__);
1620 status = smbcli_full_connection(
1621 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1622 "IPC$", NULL, anon_creds, NULL);
1623 if (!NT_STATUS_IS_OK(status)) {
1624 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1625 __location__, nt_errstr(status));
1630 if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1631 d_printf("(%s) whoami on anon connection failed\n",
1637 if (!dom_sid_equal(user_sid,
1638 dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1639 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1640 "S-1-5-7", __location__,
1641 dom_sid_string(mem_ctx, user_sid));
1645 if (!(user_creds = cli_credentials_init(mem_ctx))) {
1646 d_printf("(%s) cli_credentials_init failed\n", __location__);
1651 cli_credentials_set_conf(user_creds, global_loadparm);
1652 cli_credentials_set_username(user_creds, "torture_username",
1654 cli_credentials_set_password(user_creds,
1655 generate_random_str(user_creds, 8),
1658 if (!create_user(mem_ctx, cli, cmdline_credentials,
1659 cli_credentials_get_username(user_creds),
1660 cli_credentials_get_password(user_creds),
1661 &domain_name, &created_sid)) {
1662 d_printf("(%s) create_user failed\n", __location__);
1667 cli_credentials_set_domain(user_creds, domain_name,
1671 struct smbcli_session *session2;
1672 struct smb_composite_sesssetup setup;
1673 struct smbcli_tree *tree;
1675 session2 = smbcli_session_init(cli->transport, mem_ctx, false);
1676 if (session2 == NULL) {
1677 d_printf("(%s) smbcli_session_init failed\n",
1682 setup.in.sesskey = cli->transport->negotiate.sesskey;
1683 setup.in.capabilities = cli->transport->negotiate.capabilities;
1684 setup.in.workgroup = "";
1685 setup.in.credentials = user_creds;
1687 status = smb_composite_sesssetup(session2, &setup);
1688 if (!NT_STATUS_IS_OK(status)) {
1689 d_printf("(%s) session setup with new user failed: "
1690 "%s\n", __location__, nt_errstr(status));
1694 session2->vuid = setup.out.vuid;
1696 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1698 d_printf("(%s) secondary_tcon failed\n",
1704 if (!(user_sid = whoami(mem_ctx, tree))) {
1705 d_printf("(%s) whoami on user connection failed\n",
1714 d_printf("Created %s, found %s\n",
1715 dom_sid_string(mem_ctx, created_sid),
1716 dom_sid_string(mem_ctx, user_sid));
1718 if (!dom_sid_equal(created_sid, user_sid)) {
1723 if (!delete_user(cli, cmdline_credentials,
1724 cli_credentials_get_username(user_creds))) {
1725 d_printf("(%s) delete_user failed\n", __location__);
1730 talloc_free(mem_ctx);
1734 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1735 const char *sharename)
1738 struct srvsvc_NetShareGetInfo r;
1739 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1743 r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1744 dcerpc_server_name(p));
1745 r.in.share_name = sharename;
1747 for (i=0;i<ARRAY_SIZE(levels);i++) {
1748 r.in.level = levels[i];
1752 printf("testing NetShareGetInfo level %u on share '%s'\n",
1753 r.in.level, r.in.share_name);
1755 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1756 if (!NT_STATUS_IS_OK(status)) {
1757 printf("NetShareGetInfo level %u on share '%s' failed"
1758 " - %s\n", r.in.level, r.in.share_name,
1763 if (!W_ERROR_IS_OK(r.out.result)) {
1764 printf("NetShareGetInfo level %u on share '%s' failed "
1765 "- %s\n", r.in.level, r.in.share_name,
1766 win_errstr(r.out.result));
1775 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1776 const char **one_sharename)
1779 struct srvsvc_NetShareEnum r;
1780 struct srvsvc_NetShareCtr0 c0;
1781 uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1785 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1786 r.in.ctr.ctr0 = &c0;
1787 r.in.ctr.ctr0->count = 0;
1788 r.in.ctr.ctr0->array = NULL;
1789 r.in.max_buffer = (uint32_t)-1;
1790 r.in.resume_handle = NULL;
1792 for (i=0;i<ARRAY_SIZE(levels);i++) {
1793 r.in.level = levels[i];
1797 printf("testing NetShareEnum level %u\n", r.in.level);
1798 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1799 if (!NT_STATUS_IS_OK(status)) {
1800 printf("NetShareEnum level %u failed - %s\n",
1801 r.in.level, nt_errstr(status));
1805 if (!W_ERROR_IS_OK(r.out.result)) {
1806 printf("NetShareEnum level %u failed - %s\n",
1807 r.in.level, win_errstr(r.out.result));
1810 if (r.in.level == 0) {
1811 struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1812 if (ctr->count > 0) {
1813 *one_sharename = ctr->array[0].name;
1821 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
1823 struct dcerpc_pipe *p;
1824 TALLOC_CTX *mem_ctx;
1826 const char *sharename = NULL;
1827 struct smbcli_state *cli;
1830 if (!(mem_ctx = talloc_new(torture))) {
1834 if (!(torture_open_connection_share(
1835 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1837 talloc_free(mem_ctx);
1841 status = pipe_bind_smb(mem_ctx, cli->tree, "\\pipe\\srvsvc",
1842 &ndr_table_srvsvc, &p);
1843 if (!NT_STATUS_IS_OK(status)) {
1844 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1845 __location__, nt_errstr(status));
1850 ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1851 if (sharename == NULL) {
1852 printf("did not get sharename\n");
1854 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1858 talloc_free(mem_ctx);
1862 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
1863 struct smbcli_session *sess,
1864 const char *sharename)
1866 struct smbcli_tree *tree;
1867 TALLOC_CTX *tmp_ctx;
1868 struct dcerpc_pipe *p;
1870 struct srvsvc_NetShareGetInfo r;
1871 struct security_descriptor *result;
1873 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1874 d_printf("talloc_new failed\n");
1878 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
1879 d_printf("secondary_tcon failed\n");
1880 talloc_free(tmp_ctx);
1884 status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
1885 &ndr_table_srvsvc, &p);
1886 if (!NT_STATUS_IS_OK(status)) {
1887 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1888 __location__, nt_errstr(status));
1889 talloc_free(tmp_ctx);
1894 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
1897 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
1898 dcerpc_server_name(p));
1899 r.in.share_name = sharename;
1902 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
1903 if (!NT_STATUS_IS_OK(status)) {
1904 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
1906 talloc_free(tmp_ctx);
1910 result = talloc_steal(mem_ctx, r.out.info.info502->sd);
1911 talloc_free(tmp_ctx);
1915 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
1916 struct smbcli_session *sess,
1917 const char *sharename,
1918 struct security_descriptor *sd)
1920 struct smbcli_tree *tree;
1921 TALLOC_CTX *tmp_ctx;
1922 struct dcerpc_pipe *p;
1924 struct sec_desc_buf i;
1925 struct srvsvc_NetShareSetInfo r;
1928 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1929 d_printf("talloc_new failed\n");
1930 return NT_STATUS_NO_MEMORY;
1933 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
1934 d_printf("secondary_tcon failed\n");
1935 talloc_free(tmp_ctx);
1936 return NT_STATUS_UNSUCCESSFUL;
1939 status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
1940 &ndr_table_srvsvc, &p);
1941 if (!NT_STATUS_IS_OK(status)) {
1942 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1943 __location__, nt_errstr(status));
1944 talloc_free(tmp_ctx);
1945 return NT_STATUS_UNSUCCESSFUL;
1949 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
1952 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
1953 dcerpc_server_name(p));
1954 r.in.share_name = sharename;
1957 r.in.info.info1501 = &i;
1958 r.in.parm_error = &error;
1960 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
1961 if (!NT_STATUS_IS_OK(status)) {
1962 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
1966 talloc_free(tmp_ctx);
1970 bool try_tcon(TALLOC_CTX *mem_ctx,
1971 struct security_descriptor *orig_sd,
1972 struct smbcli_session *session,
1973 const char *sharename, const struct dom_sid *user_sid,
1974 unsigned int access_mask, NTSTATUS expected_tcon,
1975 NTSTATUS expected_mkdir)
1977 TALLOC_CTX *tmp_ctx;
1978 struct smbcli_tree *rmdir_tree, *tree;
1979 struct dom_sid *domain_sid;
1981 struct security_descriptor *sd;
1985 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1986 d_printf("talloc_new failed\n");
1990 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
1991 if (!NT_STATUS_IS_OK(status)) {
1992 d_printf("first tcon to delete dir failed\n");
1993 talloc_free(tmp_ctx);
1997 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
1999 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2000 &domain_sid, &rid))) {
2001 d_printf("dom_sid_split_rid failed\n");
2002 talloc_free(tmp_ctx);
2006 sd = security_descriptor_create(
2007 tmp_ctx, "S-1-5-32-544",
2008 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2010 dom_sid_string(mem_ctx, user_sid),
2011 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2013 d_printf("security_descriptor_create failed\n");
2014 talloc_free(tmp_ctx);
2018 status = set_sharesec(mem_ctx, session, sharename, sd);
2019 if (!NT_STATUS_IS_OK(status)) {
2020 d_printf("custom set_sharesec failed: %s\n",
2022 talloc_free(tmp_ctx);
2026 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2027 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2028 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2034 if (!NT_STATUS_IS_OK(status)) {
2035 /* An expected non-access, no point in trying to write */
2039 status = smbcli_mkdir(tree, "sharesec_testdir");
2040 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2041 d_printf("(%s) Expected %s, got %s\n", __location__,
2042 nt_errstr(expected_mkdir), nt_errstr(status));
2047 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2049 status = set_sharesec(mem_ctx, session, sharename, orig_sd);
2050 if (!NT_STATUS_IS_OK(status)) {
2051 d_printf("custom set_sharesec failed: %s\n",
2053 talloc_free(tmp_ctx);
2057 talloc_free(tmp_ctx);
2061 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2063 TALLOC_CTX *mem_ctx;
2065 struct smbcli_state *cli;
2066 struct security_descriptor *sd;
2067 struct dom_sid *user_sid;
2069 if (!(mem_ctx = talloc_new(torture))) {
2073 if (!(torture_open_connection_share(
2074 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2076 d_printf("IPC$ connection failed\n");
2077 talloc_free(mem_ctx);
2081 if (!(user_sid = whoami(mem_ctx, cli->tree))) {
2082 d_printf("whoami failed\n");
2083 talloc_free(mem_ctx);
2087 sd = get_sharesec(mem_ctx, cli->session, torture_setting_string(torture,
2090 ret &= try_tcon(mem_ctx, sd, cli->session,
2091 torture_setting_string(torture, "share", NULL),
2092 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2094 ret &= try_tcon(mem_ctx, sd, cli->session,
2095 torture_setting_string(torture, "share", NULL),
2096 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2097 NT_STATUS_MEDIA_WRITE_PROTECTED);
2099 ret &= try_tcon(mem_ctx, sd, cli->session,
2100 torture_setting_string(torture, "share", NULL),
2101 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2103 talloc_free(mem_ctx);
2107 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2109 TALLOC_CTX *mem_ctx;
2111 struct smbcli_state *cli;
2112 struct dcerpc_pipe *p;
2113 struct policy_handle lsa_handle;
2115 struct dom_sid *domain_sid;
2117 if (!(mem_ctx = talloc_new(torture))) {
2121 if (!(torture_open_connection_share(
2122 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2124 d_printf("IPC$ connection failed\n");
2125 talloc_free(mem_ctx);
2129 status = pipe_bind_smb(mem_ctx, cli->tree, "\\lsarpc",
2130 &ndr_table_lsarpc, &p);
2131 if (!NT_STATUS_IS_OK(status)) {
2132 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2134 talloc_free(mem_ctx);
2139 struct lsa_ObjectAttribute attr;
2140 struct lsa_OpenPolicy2 o;
2141 o.in.system_name = talloc_asprintf(
2142 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2145 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2146 o.out.handle = &lsa_handle;
2147 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2148 if (!NT_STATUS_IS_OK(status)) {
2149 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2150 __location__, nt_errstr(status));
2151 talloc_free(mem_ctx);
2157 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2162 int levels[] = { 2,3,5,6 };
2164 for (i=0; i<ARRAY_SIZE(levels); i++) {
2165 struct lsa_QueryInfoPolicy r;
2166 r.in.handle = &lsa_handle;
2167 r.in.level = levels[i];
2168 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2169 if (!NT_STATUS_IS_OK(status)) {
2170 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2171 "failed: %s\n", __location__,
2172 levels[i], nt_errstr(status));
2173 talloc_free(mem_ctx);
2176 if (levels[i] == 5) {
2177 domain_sid = r.out.info->account_domain.sid;
2185 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2188 struct rap_WserverGetInfo r;
2190 char servername[17];
2193 r.in.bufsize = 0xffff;
2195 status = smbcli_rap_netservergetinfo(tree, mem_ctx, &r);
2196 if (!NT_STATUS_IS_OK(status)) {
2200 memcpy(servername, r.out.info.info0.name, 16);
2201 servername[16] = '\0';
2203 if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2204 return NT_STATUS_NO_MEMORY;
2207 return NT_STATUS_OK;
2211 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct smbcli_tree *tree,
2212 const char ***printers, int *num_printers)
2214 TALLOC_CTX *mem_ctx;
2216 struct dcerpc_pipe *p;
2217 struct srvsvc_NetShareEnum r;
2218 struct srvsvc_NetShareCtr1 c1_in;
2219 struct srvsvc_NetShareCtr1 *c1;
2222 mem_ctx = talloc_new(ctx);
2223 if (mem_ctx == NULL) {
2224 return NT_STATUS_NO_MEMORY;
2227 status = pipe_bind_smb(mem_ctx, tree, "\\srvsvc", &ndr_table_srvsvc,
2229 if (!NT_STATUS_IS_OK(status)) {
2230 d_printf("could not bind to srvsvc pipe\n");
2231 talloc_free(mem_ctx);
2235 r.in.server_unc = talloc_asprintf(
2236 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2239 r.in.ctr.ctr1 = &c1_in;
2240 r.in.max_buffer = (uint32_t)-1;
2241 r.in.resume_handle = NULL;
2243 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2244 if (!NT_STATUS_IS_OK(status)) {
2245 d_printf("NetShareEnum level %u failed - %s\n",
2246 r.in.level, nt_errstr(status));
2247 talloc_free(mem_ctx);
2253 c1 = r.out.ctr.ctr1;
2254 for (i=0; i<c1->count; i++) {
2255 if (c1->array[i].type != STYPE_PRINTQ) {
2258 if (!add_string_to_array(ctx, c1->array[i].name,
2259 printers, num_printers)) {
2261 return NT_STATUS_NO_MEMORY;
2265 talloc_free(mem_ctx);
2266 return NT_STATUS_OK;
2269 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2270 const char *servername, int level, int *num_printers)
2272 struct spoolss_EnumPrinters r;
2276 r.in.flags = PRINTER_ENUM_LOCAL;
2277 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2282 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2283 if (!NT_STATUS_IS_OK(status)) {
2284 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2285 __location__, nt_errstr(status));
2289 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2290 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2291 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2292 win_errstr(r.out.result));
2296 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2297 if (blob.data == NULL) {
2298 d_printf("(%s) data_blob_talloc failed\n", __location__);
2302 r.in.buffer = &blob;
2303 r.in.offered = r.out.needed;
2305 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2306 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2307 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2308 "%s\n", __location__, nt_errstr(status),
2309 win_errstr(r.out.result));
2313 *num_printers = r.out.count;
2318 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2319 struct policy_handle *handle, int level,
2320 union spoolss_PrinterInfo **res)
2322 TALLOC_CTX *mem_ctx;
2323 struct spoolss_GetPrinter r;
2327 mem_ctx = talloc_new(ctx);
2328 if (mem_ctx == NULL) {
2329 return NT_STATUS_NO_MEMORY;
2332 r.in.handle = handle;
2337 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2338 if (!NT_STATUS_IS_OK(status)) {
2339 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2340 __location__, nt_errstr(status));
2341 talloc_free(mem_ctx);
2345 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2346 printf("GetPrinter unexpected return code %s, should "
2347 "be WERR_INSUFFICIENT_BUFFER\n",
2348 win_errstr(r.out.result));
2349 talloc_free(mem_ctx);
2350 return NT_STATUS_UNSUCCESSFUL;
2353 r.in.handle = handle;
2355 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2356 if (blob.data == NULL) {
2357 talloc_free(mem_ctx);
2358 return NT_STATUS_NO_MEMORY;
2360 memset(blob.data, 0, blob.length);
2361 r.in.buffer = &blob;
2362 r.in.offered = r.out.needed;
2364 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2365 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2366 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2367 "%s\n", __location__, nt_errstr(status),
2368 win_errstr(r.out.result));
2369 talloc_free(mem_ctx);
2370 return NT_STATUS_IS_OK(status) ?
2371 NT_STATUS_UNSUCCESSFUL : status;
2375 *res = talloc_steal(ctx, r.out.info);
2378 talloc_free(mem_ctx);
2379 return NT_STATUS_OK;
2382 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2384 TALLOC_CTX *mem_ctx;
2386 struct smbcli_state *cli;
2387 struct dcerpc_pipe *p;
2389 struct policy_handle server_handle, printer_handle;
2390 const char **printers;
2392 struct spoolss_UserLevel1 userlevel1;
2395 if (!(mem_ctx = talloc_new(torture))) {
2399 if (!(torture_open_connection_share(
2400 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2402 d_printf("IPC$ connection failed\n");
2403 talloc_free(mem_ctx);
2407 status = get_servername(mem_ctx, cli->tree, &servername);
2408 if (!NT_STATUS_IS_OK(status)) {
2409 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2410 __location__, nt_errstr(status));
2411 talloc_free(mem_ctx);
2415 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, cli->tree,
2416 &printers, &num_printers))) {
2417 talloc_free(mem_ctx);
2421 if (num_printers == 0) {
2422 d_printf("Did not find printers\n");
2423 talloc_free(mem_ctx);
2427 status = pipe_bind_smb(mem_ctx, cli->tree, "\\spoolss",
2428 &ndr_table_spoolss, &p);
2429 if (!NT_STATUS_IS_OK(status)) {
2430 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2432 talloc_free(mem_ctx);
2436 ZERO_STRUCT(userlevel1);
2437 userlevel1.client = talloc_asprintf(
2438 mem_ctx, "\\\\%s", lp_netbios_name(global_loadparm));
2439 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2440 userlevel1.build = 2600;
2441 userlevel1.major = 3;
2442 userlevel1.minor = 0;
2443 userlevel1.processor = 0;
2446 struct spoolss_OpenPrinterEx r;
2449 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2451 r.in.datatype = NULL;
2452 r.in.access_mask = 0;
2454 r.in.userlevel.level1 = &userlevel1;
2455 r.out.handle = &server_handle;
2457 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2458 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2459 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2460 "%s, %s\n", __location__, nt_errstr(status),
2461 win_errstr(r.out.result));
2462 talloc_free(mem_ctx);
2468 struct spoolss_ClosePrinter r;
2470 r.in.handle = &server_handle;
2471 r.out.handle = &server_handle;
2473 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2474 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2475 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2476 "%s, %s\n", __location__, nt_errstr(status),
2477 win_errstr(r.out.result));
2478 talloc_free(mem_ctx);
2484 struct spoolss_OpenPrinterEx r;
2487 r.in.printername = talloc_asprintf(
2488 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2489 r.in.datatype = NULL;
2490 r.in.access_mask = 0;
2492 r.in.userlevel.level1 = &userlevel1;
2493 r.out.handle = &printer_handle;
2495 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2496 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2497 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2498 "%s, %s\n", __location__, nt_errstr(status),
2499 win_errstr(r.out.result));
2500 talloc_free(mem_ctx);
2508 for (i=0; i<8; i++) {
2509 status = getprinterinfo(mem_ctx, p, &printer_handle,
2511 if (!NT_STATUS_IS_OK(status)) {
2512 d_printf("(%s) getprinterinfo %d failed: %s\n",
2513 __location__, i, nt_errstr(status));
2520 struct spoolss_ClosePrinter r;
2522 r.in.handle = &printer_handle;
2523 r.out.handle = &printer_handle;
2525 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2526 if (!NT_STATUS_IS_OK(status)) {
2527 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2528 "%s\n", __location__, nt_errstr(status));
2529 talloc_free(mem_ctx);
2536 if (!enumprinters(mem_ctx, p, servername, 1,
2538 d_printf("(%s) enumprinters failed\n", __location__);
2539 talloc_free(mem_ctx);
2542 if (num_printers != num_enumerated) {
2543 d_printf("(%s) netshareenum gave %d printers, "
2544 "enumprinters lvl 1 gave %d\n", __location__,
2545 num_printers, num_enumerated);
2546 talloc_free(mem_ctx);
2553 if (!enumprinters(mem_ctx, p, servername, 2,
2555 d_printf("(%s) enumprinters failed\n", __location__);
2556 talloc_free(mem_ctx);
2559 if (num_printers != num_enumerated) {
2560 d_printf("(%s) netshareenum gave %d printers, "
2561 "enumprinters lvl 2 gave %d\n", __location__,
2562 num_printers, num_enumerated);
2563 talloc_free(mem_ctx);
2568 talloc_free(mem_ctx);
2573 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2575 TALLOC_CTX *mem_ctx;
2576 struct smbcli_state *cli;
2577 struct dcerpc_pipe *p;
2581 if (!(mem_ctx = talloc_new(torture))) {
2585 if (!(torture_open_connection_share(
2586 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2588 d_printf("IPC$ connection failed\n");
2589 talloc_free(mem_ctx);
2593 status = get_servername(mem_ctx, cli->tree, &servername);
2594 if (!NT_STATUS_IS_OK(status)) {
2595 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2596 __location__, nt_errstr(status));
2597 talloc_free(mem_ctx);
2601 status = pipe_bind_smb(mem_ctx, cli->tree, "\\wkssvc",
2602 &ndr_table_wkssvc, &p);
2603 if (!NT_STATUS_IS_OK(status)) {
2604 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2606 talloc_free(mem_ctx);
2611 struct wkssvc_NetWkstaInfo100 wks100;
2612 union wkssvc_NetWkstaInfo info;
2613 struct wkssvc_NetWkstaGetInfo r;
2615 r.in.server_name = "\\foo";
2617 info.info100 = &wks100;
2620 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2621 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2622 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2623 "%s, %s\n", __location__, nt_errstr(status),
2624 win_errstr(r.out.result));
2625 talloc_free(mem_ctx);
2629 if (strcmp(servername,
2630 r.out.info->info100->server_name) != 0) {
2631 d_printf("(%s) servername inconsistency: RAP=%s, "
2632 "dcerpc_wkssvc_NetWksGetInfo=%s",
2633 __location__, servername,
2634 r.out.info->info100->server_name);
2635 talloc_free(mem_ctx);
2640 talloc_free(mem_ctx);
2644 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2645 struct policy_handle *handle)
2647 struct winreg_CloseKey c;
2649 TALLOC_CTX *mem_ctx;
2651 c.in.handle = c.out.handle = handle;
2653 if (!(mem_ctx = talloc_new(p))) {
2654 return NT_STATUS_NO_MEMORY;
2657 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2658 talloc_free(mem_ctx);
2660 if (!NT_STATUS_IS_OK(status)) {
2664 if (!W_ERROR_IS_OK(c.out.result)) {
2665 return werror_to_ntstatus(c.out.result);
2668 return NT_STATUS_OK;
2671 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2672 TALLOC_CTX *mem_ctx)
2674 uint32_t enum_index = 0;
2677 struct winreg_EnumValue r;
2678 struct winreg_StringBuf name;
2679 enum winreg_Type type = 0;
2682 uint32_t size, length;
2684 r.in.handle = handle;
2685 r.in.enum_index = enum_index;
2688 r.in.name = r.out.name = &name;
2694 r.in.length = &length;
2696 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
2697 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2698 return NT_STATUS_OK;
2704 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
2705 TALLOC_CTX *mem_ctx, int depth)
2707 struct winreg_EnumKey r;
2708 struct winreg_StringBuf class, name;
2713 return NT_STATUS_OK;
2719 r.in.handle = handle;
2720 r.in.enum_index = 0;
2722 r.in.keyclass = &class;
2724 r.in.last_changed_time = &t;
2727 TALLOC_CTX *tmp_ctx;
2728 struct winreg_OpenKey o;
2729 struct policy_handle key_handle;
2732 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2733 return NT_STATUS_NO_MEMORY;
2739 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
2740 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2741 /* We're done enumerating */
2742 talloc_free(tmp_ctx);
2743 return NT_STATUS_OK;
2746 for (i=0; i<10-depth; i++)
2748 printf("%s\n", r.out.name->name);
2751 o.in.parent_handle = handle;
2752 o.in.keyname.name = r.out.name->name;
2754 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2755 o.out.handle = &key_handle;
2757 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
2758 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
2759 enumkeys(p, &key_handle, tmp_ctx, depth-1);
2760 enumvalues(p, &key_handle, tmp_ctx);
2761 status = winreg_close(p, &key_handle);
2762 if (!NT_STATUS_IS_OK(status)) {
2767 talloc_free(tmp_ctx);
2769 r.in.enum_index += 1;
2772 return NT_STATUS_OK;
2775 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
2777 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
2778 const char *name, winreg_open_fn open_fn)
2780 struct policy_handle handle;
2781 struct winreg_OpenHKLM r;
2784 r.in.system_name = 0;
2785 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2786 r.out.handle = &handle;
2788 status = open_fn(p, mem_ctx, &r);
2789 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2790 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
2791 nt_errstr(status), win_errstr(r.out.result));
2795 enumkeys(p, &handle, mem_ctx, 4);
2797 status = winreg_close(p, &handle);
2798 if (!NT_STATUS_IS_OK(status)) {
2799 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
2800 __location__, nt_errstr(status));
2807 bool torture_samba3_rpc_winreg(struct torture_context *torture)
2810 struct dcerpc_pipe *p;
2811 TALLOC_CTX *mem_ctx;
2817 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
2818 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
2819 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
2820 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
2821 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
2826 mem_ctx = talloc_init("torture_rpc_winreg");
2828 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
2830 if (!NT_STATUS_IS_OK(status)) {
2831 talloc_free(mem_ctx);
2836 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
2838 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
2839 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
2844 talloc_free(mem_ctx);