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 %s\n", nt_errstr(status));
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);
1863 * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
1864 * NT_STATUS_NO_SAM_ACCOUNT
1867 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
1869 TALLOC_CTX *mem_ctx;
1870 struct dcerpc_pipe *net_pipe;
1872 bool result = false;
1874 struct netr_ServerReqChallenge r;
1875 struct netr_Credential netr_cli_creds;
1876 struct netr_Credential netr_srv_creds;
1877 uint32_t negotiate_flags;
1878 struct netr_ServerAuthenticate2 a;
1879 struct creds_CredentialState *creds_state;
1880 struct netr_Credential netr_cred;
1881 struct samr_Password mach_pw;
1882 struct smbcli_state *cli;
1884 if (!(mem_ctx = talloc_new(torture))) {
1885 d_printf("talloc_new failed\n");
1889 if (!(wksname = generate_random_str_list(
1890 mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
1891 d_printf("generate_random_str_list failed\n");
1895 if (!(torture_open_connection_share(
1897 torture_setting_string(torture, "host", NULL),
1899 d_printf("IPC$ connection failed\n");
1903 if (!(net_pipe = dcerpc_pipe_init(
1904 mem_ctx, cli->transport->socket->event.ctx))) {
1905 d_printf("dcerpc_pipe_init failed\n");
1909 status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
1910 if (!NT_STATUS_IS_OK(status)) {
1911 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1916 status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1917 if (!NT_STATUS_IS_OK(status)) {
1918 d_printf("dcerpc_bind_auth_none failed: %s\n",
1923 r.in.computer_name = wksname;
1924 r.in.server_name = talloc_asprintf(
1925 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1926 if (r.in.server_name == NULL) {
1927 d_printf("talloc_asprintf failed\n");
1930 generate_random_buffer(netr_cli_creds.data,
1931 sizeof(netr_cli_creds.data));
1932 r.in.credentials = &netr_cli_creds;
1933 r.out.credentials = &netr_srv_creds;
1935 status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
1936 if (!NT_STATUS_IS_OK(status)) {
1937 d_printf("netr_ServerReqChallenge failed: %s\n",
1942 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
1943 E_md4hash("foobar", mach_pw.hash);
1945 creds_state = talloc(mem_ctx, struct creds_CredentialState);
1946 creds_client_init(creds_state, r.in.credentials,
1947 r.out.credentials, &mach_pw,
1948 &netr_cred, negotiate_flags);
1950 a.in.server_name = talloc_asprintf(
1951 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1952 a.in.account_name = talloc_asprintf(
1953 mem_ctx, "%s$", wksname);
1954 a.in.computer_name = wksname;
1955 a.in.secure_channel_type = SEC_CHAN_WKSTA;
1956 a.in.negotiate_flags = &negotiate_flags;
1957 a.out.negotiate_flags = &negotiate_flags;
1958 a.in.credentials = &netr_cred;
1959 a.out.credentials = &netr_cred;
1961 status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
1963 if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
1964 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
1965 "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
1972 talloc_free(mem_ctx);
1976 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
1977 struct smbcli_session *sess,
1978 const char *sharename)
1980 struct smbcli_tree *tree;
1981 TALLOC_CTX *tmp_ctx;
1982 struct dcerpc_pipe *p;
1984 struct srvsvc_NetShareGetInfo r;
1985 struct security_descriptor *result;
1987 if (!(tmp_ctx = talloc_new(mem_ctx))) {
1988 d_printf("talloc_new failed\n");
1992 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
1993 d_printf("secondary_tcon failed\n");
1994 talloc_free(tmp_ctx);
1998 status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
1999 &ndr_table_srvsvc, &p);
2000 if (!NT_STATUS_IS_OK(status)) {
2001 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2002 __location__, nt_errstr(status));
2003 talloc_free(tmp_ctx);
2008 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2011 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2012 dcerpc_server_name(p));
2013 r.in.share_name = sharename;
2016 status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2017 if (!NT_STATUS_IS_OK(status)) {
2018 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2020 talloc_free(tmp_ctx);
2024 result = talloc_steal(mem_ctx, r.out.info.info502->sd);
2025 talloc_free(tmp_ctx);
2029 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2030 struct smbcli_session *sess,
2031 const char *sharename,
2032 struct security_descriptor *sd)
2034 struct smbcli_tree *tree;
2035 TALLOC_CTX *tmp_ctx;
2036 struct dcerpc_pipe *p;
2038 struct sec_desc_buf i;
2039 struct srvsvc_NetShareSetInfo r;
2042 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2043 d_printf("talloc_new failed\n");
2044 return NT_STATUS_NO_MEMORY;
2047 if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2048 d_printf("secondary_tcon failed\n");
2049 talloc_free(tmp_ctx);
2050 return NT_STATUS_UNSUCCESSFUL;
2053 status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
2054 &ndr_table_srvsvc, &p);
2055 if (!NT_STATUS_IS_OK(status)) {
2056 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2057 __location__, nt_errstr(status));
2058 talloc_free(tmp_ctx);
2059 return NT_STATUS_UNSUCCESSFUL;
2063 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2066 r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2067 dcerpc_server_name(p));
2068 r.in.share_name = sharename;
2071 r.in.info.info1501 = &i;
2072 r.in.parm_error = &error;
2074 status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2075 if (!NT_STATUS_IS_OK(status)) {
2076 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2080 talloc_free(tmp_ctx);
2084 bool try_tcon(TALLOC_CTX *mem_ctx,
2085 struct security_descriptor *orig_sd,
2086 struct smbcli_session *session,
2087 const char *sharename, const struct dom_sid *user_sid,
2088 unsigned int access_mask, NTSTATUS expected_tcon,
2089 NTSTATUS expected_mkdir)
2091 TALLOC_CTX *tmp_ctx;
2092 struct smbcli_tree *rmdir_tree, *tree;
2093 struct dom_sid *domain_sid;
2095 struct security_descriptor *sd;
2099 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2100 d_printf("talloc_new failed\n");
2104 status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2105 if (!NT_STATUS_IS_OK(status)) {
2106 d_printf("first tcon to delete dir failed\n");
2107 talloc_free(tmp_ctx);
2111 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2113 if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2114 &domain_sid, &rid))) {
2115 d_printf("dom_sid_split_rid failed\n");
2116 talloc_free(tmp_ctx);
2120 sd = security_descriptor_dacl_create(
2121 tmp_ctx, 0, "S-1-5-32-544",
2122 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2124 dom_sid_string(mem_ctx, user_sid),
2125 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2127 d_printf("security_descriptor_dacl_create failed\n");
2128 talloc_free(tmp_ctx);
2132 status = set_sharesec(mem_ctx, session, sharename, sd);
2133 if (!NT_STATUS_IS_OK(status)) {
2134 d_printf("custom set_sharesec failed: %s\n",
2136 talloc_free(tmp_ctx);
2140 status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2141 if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2142 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2148 if (!NT_STATUS_IS_OK(status)) {
2149 /* An expected non-access, no point in trying to write */
2153 status = smbcli_mkdir(tree, "sharesec_testdir");
2154 if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2155 d_printf("(%s) Expected %s, got %s\n", __location__,
2156 nt_errstr(expected_mkdir), nt_errstr(status));
2161 smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2163 status = set_sharesec(mem_ctx, session, sharename, orig_sd);
2164 if (!NT_STATUS_IS_OK(status)) {
2165 d_printf("custom set_sharesec failed: %s\n",
2167 talloc_free(tmp_ctx);
2171 talloc_free(tmp_ctx);
2175 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2177 TALLOC_CTX *mem_ctx;
2179 struct smbcli_state *cli;
2180 struct security_descriptor *sd;
2181 struct dom_sid *user_sid;
2183 if (!(mem_ctx = talloc_new(torture))) {
2187 if (!(torture_open_connection_share(
2188 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2190 d_printf("IPC$ connection failed\n");
2191 talloc_free(mem_ctx);
2195 if (!(user_sid = whoami(mem_ctx, cli->tree))) {
2196 d_printf("whoami failed\n");
2197 talloc_free(mem_ctx);
2201 sd = get_sharesec(mem_ctx, cli->session, torture_setting_string(torture,
2204 ret &= try_tcon(mem_ctx, sd, cli->session,
2205 torture_setting_string(torture, "share", NULL),
2206 user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2208 ret &= try_tcon(mem_ctx, sd, cli->session,
2209 torture_setting_string(torture, "share", NULL),
2210 user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2211 NT_STATUS_MEDIA_WRITE_PROTECTED);
2213 ret &= try_tcon(mem_ctx, sd, cli->session,
2214 torture_setting_string(torture, "share", NULL),
2215 user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2217 talloc_free(mem_ctx);
2221 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2223 TALLOC_CTX *mem_ctx;
2225 struct smbcli_state *cli;
2226 struct dcerpc_pipe *p;
2227 struct policy_handle lsa_handle;
2229 struct dom_sid *domain_sid;
2231 if (!(mem_ctx = talloc_new(torture))) {
2235 if (!(torture_open_connection_share(
2236 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2238 d_printf("IPC$ connection failed\n");
2239 talloc_free(mem_ctx);
2243 status = pipe_bind_smb(mem_ctx, cli->tree, "\\lsarpc",
2244 &ndr_table_lsarpc, &p);
2245 if (!NT_STATUS_IS_OK(status)) {
2246 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2248 talloc_free(mem_ctx);
2253 struct lsa_ObjectAttribute attr;
2254 struct lsa_OpenPolicy2 o;
2255 o.in.system_name = talloc_asprintf(
2256 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2259 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2260 o.out.handle = &lsa_handle;
2261 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2262 if (!NT_STATUS_IS_OK(status)) {
2263 d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2264 __location__, nt_errstr(status));
2265 talloc_free(mem_ctx);
2271 p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2276 int levels[] = { 2,3,5,6 };
2278 for (i=0; i<ARRAY_SIZE(levels); i++) {
2279 struct lsa_QueryInfoPolicy r;
2280 r.in.handle = &lsa_handle;
2281 r.in.level = levels[i];
2282 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2283 if (!NT_STATUS_IS_OK(status)) {
2284 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2285 "failed: %s\n", __location__,
2286 levels[i], nt_errstr(status));
2287 talloc_free(mem_ctx);
2290 if (levels[i] == 5) {
2291 domain_sid = r.out.info->account_domain.sid;
2299 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2302 struct rap_WserverGetInfo r;
2304 char servername[17];
2307 r.in.bufsize = 0xffff;
2309 status = smbcli_rap_netservergetinfo(tree, mem_ctx, &r);
2310 if (!NT_STATUS_IS_OK(status)) {
2314 memcpy(servername, r.out.info.info0.name, 16);
2315 servername[16] = '\0';
2317 if (pull_ascii_talloc(mem_ctx, name, servername) < 0) {
2318 return NT_STATUS_NO_MEMORY;
2321 return NT_STATUS_OK;
2325 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct smbcli_tree *tree,
2326 const char ***printers, int *num_printers)
2328 TALLOC_CTX *mem_ctx;
2330 struct dcerpc_pipe *p;
2331 struct srvsvc_NetShareEnum r;
2332 struct srvsvc_NetShareCtr1 c1_in;
2333 struct srvsvc_NetShareCtr1 *c1;
2336 mem_ctx = talloc_new(ctx);
2337 if (mem_ctx == NULL) {
2338 return NT_STATUS_NO_MEMORY;
2341 status = pipe_bind_smb(mem_ctx, tree, "\\srvsvc", &ndr_table_srvsvc,
2343 if (!NT_STATUS_IS_OK(status)) {
2344 d_printf("could not bind to srvsvc pipe\n");
2345 talloc_free(mem_ctx);
2349 r.in.server_unc = talloc_asprintf(
2350 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2353 r.in.ctr.ctr1 = &c1_in;
2354 r.in.max_buffer = (uint32_t)-1;
2355 r.in.resume_handle = NULL;
2357 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2358 if (!NT_STATUS_IS_OK(status)) {
2359 d_printf("NetShareEnum level %u failed - %s\n",
2360 r.in.level, nt_errstr(status));
2361 talloc_free(mem_ctx);
2367 c1 = r.out.ctr.ctr1;
2368 for (i=0; i<c1->count; i++) {
2369 if (c1->array[i].type != STYPE_PRINTQ) {
2372 if (!add_string_to_array(ctx, c1->array[i].name,
2373 printers, num_printers)) {
2375 return NT_STATUS_NO_MEMORY;
2379 talloc_free(mem_ctx);
2380 return NT_STATUS_OK;
2383 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2384 const char *servername, int level, int *num_printers)
2386 struct spoolss_EnumPrinters r;
2390 r.in.flags = PRINTER_ENUM_LOCAL;
2391 r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2396 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2397 if (!NT_STATUS_IS_OK(status)) {
2398 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2399 __location__, nt_errstr(status));
2403 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2404 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2405 "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2406 win_errstr(r.out.result));
2410 blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2411 if (blob.data == NULL) {
2412 d_printf("(%s) data_blob_talloc failed\n", __location__);
2416 r.in.buffer = &blob;
2417 r.in.offered = r.out.needed;
2419 status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2420 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2421 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2422 "%s\n", __location__, nt_errstr(status),
2423 win_errstr(r.out.result));
2427 *num_printers = r.out.count;
2432 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2433 struct policy_handle *handle, int level,
2434 union spoolss_PrinterInfo **res)
2436 TALLOC_CTX *mem_ctx;
2437 struct spoolss_GetPrinter r;
2441 mem_ctx = talloc_new(ctx);
2442 if (mem_ctx == NULL) {
2443 return NT_STATUS_NO_MEMORY;
2446 r.in.handle = handle;
2451 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2452 if (!NT_STATUS_IS_OK(status)) {
2453 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2454 __location__, nt_errstr(status));
2455 talloc_free(mem_ctx);
2459 if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2460 printf("GetPrinter unexpected return code %s, should "
2461 "be WERR_INSUFFICIENT_BUFFER\n",
2462 win_errstr(r.out.result));
2463 talloc_free(mem_ctx);
2464 return NT_STATUS_UNSUCCESSFUL;
2467 r.in.handle = handle;
2469 blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2470 if (blob.data == NULL) {
2471 talloc_free(mem_ctx);
2472 return NT_STATUS_NO_MEMORY;
2474 memset(blob.data, 0, blob.length);
2475 r.in.buffer = &blob;
2476 r.in.offered = r.out.needed;
2478 status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2479 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2480 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2481 "%s\n", __location__, nt_errstr(status),
2482 win_errstr(r.out.result));
2483 talloc_free(mem_ctx);
2484 return NT_STATUS_IS_OK(status) ?
2485 NT_STATUS_UNSUCCESSFUL : status;
2489 *res = talloc_steal(ctx, r.out.info);
2492 talloc_free(mem_ctx);
2493 return NT_STATUS_OK;
2496 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2498 TALLOC_CTX *mem_ctx;
2500 struct smbcli_state *cli;
2501 struct dcerpc_pipe *p;
2503 struct policy_handle server_handle, printer_handle;
2504 const char **printers;
2506 struct spoolss_UserLevel1 userlevel1;
2509 if (!(mem_ctx = talloc_new(torture))) {
2513 if (!(torture_open_connection_share(
2514 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2516 d_printf("IPC$ connection failed\n");
2517 talloc_free(mem_ctx);
2521 status = get_servername(mem_ctx, cli->tree, &servername);
2522 if (!NT_STATUS_IS_OK(status)) {
2523 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2524 __location__, nt_errstr(status));
2525 talloc_free(mem_ctx);
2529 if (!NT_STATUS_IS_OK(find_printers(mem_ctx, cli->tree,
2530 &printers, &num_printers))) {
2531 talloc_free(mem_ctx);
2535 if (num_printers == 0) {
2536 d_printf("Did not find printers\n");
2537 talloc_free(mem_ctx);
2541 status = pipe_bind_smb(mem_ctx, cli->tree, "\\spoolss",
2542 &ndr_table_spoolss, &p);
2543 if (!NT_STATUS_IS_OK(status)) {
2544 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2546 talloc_free(mem_ctx);
2550 ZERO_STRUCT(userlevel1);
2551 userlevel1.client = talloc_asprintf(
2552 mem_ctx, "\\\\%s", lp_netbios_name(global_loadparm));
2553 userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2554 userlevel1.build = 2600;
2555 userlevel1.major = 3;
2556 userlevel1.minor = 0;
2557 userlevel1.processor = 0;
2560 struct spoolss_OpenPrinterEx r;
2563 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2565 r.in.datatype = NULL;
2566 r.in.access_mask = 0;
2568 r.in.userlevel.level1 = &userlevel1;
2569 r.out.handle = &server_handle;
2571 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2572 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2573 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2574 "%s, %s\n", __location__, nt_errstr(status),
2575 win_errstr(r.out.result));
2576 talloc_free(mem_ctx);
2582 struct spoolss_ClosePrinter r;
2584 r.in.handle = &server_handle;
2585 r.out.handle = &server_handle;
2587 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2588 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2589 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2590 "%s, %s\n", __location__, nt_errstr(status),
2591 win_errstr(r.out.result));
2592 talloc_free(mem_ctx);
2598 struct spoolss_OpenPrinterEx r;
2601 r.in.printername = talloc_asprintf(
2602 mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2603 r.in.datatype = NULL;
2604 r.in.access_mask = 0;
2606 r.in.userlevel.level1 = &userlevel1;
2607 r.out.handle = &printer_handle;
2609 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2610 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2611 d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2612 "%s, %s\n", __location__, nt_errstr(status),
2613 win_errstr(r.out.result));
2614 talloc_free(mem_ctx);
2622 for (i=0; i<8; i++) {
2623 status = getprinterinfo(mem_ctx, p, &printer_handle,
2625 if (!NT_STATUS_IS_OK(status)) {
2626 d_printf("(%s) getprinterinfo %d failed: %s\n",
2627 __location__, i, nt_errstr(status));
2634 struct spoolss_ClosePrinter r;
2636 r.in.handle = &printer_handle;
2637 r.out.handle = &printer_handle;
2639 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2640 if (!NT_STATUS_IS_OK(status)) {
2641 d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2642 "%s\n", __location__, nt_errstr(status));
2643 talloc_free(mem_ctx);
2650 if (!enumprinters(mem_ctx, p, servername, 1,
2652 d_printf("(%s) enumprinters failed\n", __location__);
2653 talloc_free(mem_ctx);
2656 if (num_printers != num_enumerated) {
2657 d_printf("(%s) netshareenum gave %d printers, "
2658 "enumprinters lvl 1 gave %d\n", __location__,
2659 num_printers, num_enumerated);
2660 talloc_free(mem_ctx);
2667 if (!enumprinters(mem_ctx, p, servername, 2,
2669 d_printf("(%s) enumprinters failed\n", __location__);
2670 talloc_free(mem_ctx);
2673 if (num_printers != num_enumerated) {
2674 d_printf("(%s) netshareenum gave %d printers, "
2675 "enumprinters lvl 2 gave %d\n", __location__,
2676 num_printers, num_enumerated);
2677 talloc_free(mem_ctx);
2682 talloc_free(mem_ctx);
2687 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2689 TALLOC_CTX *mem_ctx;
2690 struct smbcli_state *cli;
2691 struct dcerpc_pipe *p;
2695 if (!(mem_ctx = talloc_new(torture))) {
2699 if (!(torture_open_connection_share(
2700 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
2702 d_printf("IPC$ connection failed\n");
2703 talloc_free(mem_ctx);
2707 status = get_servername(mem_ctx, cli->tree, &servername);
2708 if (!NT_STATUS_IS_OK(status)) {
2709 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2710 __location__, nt_errstr(status));
2711 talloc_free(mem_ctx);
2715 status = pipe_bind_smb(mem_ctx, cli->tree, "\\wkssvc",
2716 &ndr_table_wkssvc, &p);
2717 if (!NT_STATUS_IS_OK(status)) {
2718 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2720 talloc_free(mem_ctx);
2725 struct wkssvc_NetWkstaInfo100 wks100;
2726 union wkssvc_NetWkstaInfo info;
2727 struct wkssvc_NetWkstaGetInfo r;
2729 r.in.server_name = "\\foo";
2731 info.info100 = &wks100;
2734 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2735 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2736 d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2737 "%s, %s\n", __location__, nt_errstr(status),
2738 win_errstr(r.out.result));
2739 talloc_free(mem_ctx);
2743 if (strcmp(servername,
2744 r.out.info->info100->server_name) != 0) {
2745 d_printf("(%s) servername inconsistency: RAP=%s, "
2746 "dcerpc_wkssvc_NetWksGetInfo=%s",
2747 __location__, servername,
2748 r.out.info->info100->server_name);
2749 talloc_free(mem_ctx);
2754 talloc_free(mem_ctx);
2758 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2759 struct policy_handle *handle)
2761 struct winreg_CloseKey c;
2763 TALLOC_CTX *mem_ctx;
2765 c.in.handle = c.out.handle = handle;
2767 if (!(mem_ctx = talloc_new(p))) {
2768 return NT_STATUS_NO_MEMORY;
2771 status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2772 talloc_free(mem_ctx);
2774 if (!NT_STATUS_IS_OK(status)) {
2778 if (!W_ERROR_IS_OK(c.out.result)) {
2779 return werror_to_ntstatus(c.out.result);
2782 return NT_STATUS_OK;
2785 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2786 TALLOC_CTX *mem_ctx)
2788 uint32_t enum_index = 0;
2791 struct winreg_EnumValue r;
2792 struct winreg_StringBuf name;
2793 enum winreg_Type type = 0;
2796 uint32_t size, length;
2798 r.in.handle = handle;
2799 r.in.enum_index = enum_index;
2802 r.in.name = r.out.name = &name;
2808 r.in.length = &length;
2810 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
2811 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2812 return NT_STATUS_OK;
2818 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle,
2819 TALLOC_CTX *mem_ctx, int depth)
2821 struct winreg_EnumKey r;
2822 struct winreg_StringBuf class, name;
2827 return NT_STATUS_OK;
2833 r.in.handle = handle;
2834 r.in.enum_index = 0;
2836 r.in.keyclass = &class;
2838 r.in.last_changed_time = &t;
2841 TALLOC_CTX *tmp_ctx;
2842 struct winreg_OpenKey o;
2843 struct policy_handle key_handle;
2846 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2847 return NT_STATUS_NO_MEMORY;
2853 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
2854 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2855 /* We're done enumerating */
2856 talloc_free(tmp_ctx);
2857 return NT_STATUS_OK;
2860 for (i=0; i<10-depth; i++)
2862 printf("%s\n", r.out.name->name);
2865 o.in.parent_handle = handle;
2866 o.in.keyname.name = r.out.name->name;
2868 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2869 o.out.handle = &key_handle;
2871 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
2872 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
2873 enumkeys(p, &key_handle, tmp_ctx, depth-1);
2874 enumvalues(p, &key_handle, tmp_ctx);
2875 status = winreg_close(p, &key_handle);
2876 if (!NT_STATUS_IS_OK(status)) {
2881 talloc_free(tmp_ctx);
2883 r.in.enum_index += 1;
2886 return NT_STATUS_OK;
2889 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
2891 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
2892 const char *name, winreg_open_fn open_fn)
2894 struct policy_handle handle;
2895 struct winreg_OpenHKLM r;
2898 r.in.system_name = 0;
2899 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2900 r.out.handle = &handle;
2902 status = open_fn(p, mem_ctx, &r);
2903 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2904 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
2905 nt_errstr(status), win_errstr(r.out.result));
2909 enumkeys(p, &handle, mem_ctx, 4);
2911 status = winreg_close(p, &handle);
2912 if (!NT_STATUS_IS_OK(status)) {
2913 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
2914 __location__, nt_errstr(status));
2921 bool torture_samba3_rpc_winreg(struct torture_context *torture)
2924 struct dcerpc_pipe *p;
2925 TALLOC_CTX *mem_ctx;
2931 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
2932 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
2933 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
2934 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
2935 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
2940 mem_ctx = talloc_init("torture_rpc_winreg");
2942 status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
2944 if (!NT_STATUS_IS_OK(status)) {
2945 talloc_free(mem_ctx);
2950 ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
2952 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
2953 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
2958 talloc_free(mem_ctx);