2 Unix SMB/CIFS implementation.
3 test suite for lsa rpc operations
5 Copyright (C) Andrew Tridgell 2003
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
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 "librpc/gen_ndr/ndr_lsa_c.h"
25 #include "libcli/auth/libcli_auth.h"
26 #include "torture/rpc/torture_rpc.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "param/param.h"
30 static void init_lsa_String(struct lsa_String
*name
, const char *s
)
35 static bool test_CreateSecret_basic(struct dcerpc_pipe
*p
,
36 struct torture_context
*tctx
,
37 struct policy_handle
*handle
)
40 struct lsa_CreateSecret r
;
41 struct lsa_SetSecret r3
;
42 struct lsa_QuerySecret r4
;
43 struct policy_handle sec_handle
;
44 struct lsa_DeleteObject d
;
45 struct lsa_DATA_BUF buf1
;
46 struct lsa_DATA_BUF_PTR bufp1
;
48 DATA_BLOB session_key
;
49 NTTIME old_mtime
, new_mtime
;
50 DATA_BLOB blob1
, blob2
;
51 const char *secret1
= "abcdef12345699qwerty";
54 struct dcerpc_binding_handle
*b
= p
->binding_handle
;
56 secname
= talloc_asprintf(tctx
, "torturesecret-%u", (unsigned int)random());
58 torture_comment(tctx
, "Testing CreateSecret of %s\n", secname
);
60 init_lsa_String(&r
.in
.name
, secname
);
63 r
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
64 r
.out
.sec_handle
= &sec_handle
;
66 torture_assert_ntstatus_ok(tctx
, dcerpc_lsa_CreateSecret_r(b
, tctx
, &r
),
67 "CreateSecret failed");
68 torture_assert_ntstatus_ok(tctx
, r
.out
.result
, "CreateSecret failed");
70 status
= dcerpc_fetch_session_key(p
, &session_key
);
71 torture_assert_ntstatus_ok(tctx
, status
, "dcerpc_fetch_session_key failed");
73 enc_key
= sess_encrypt_string(secret1
, &session_key
);
75 r3
.in
.sec_handle
= &sec_handle
;
76 r3
.in
.new_val
= &buf1
;
78 r3
.in
.new_val
->data
= enc_key
.data
;
79 r3
.in
.new_val
->length
= enc_key
.length
;
80 r3
.in
.new_val
->size
= enc_key
.length
;
82 torture_comment(tctx
, "Testing SetSecret\n");
84 torture_assert_ntstatus_ok(tctx
, dcerpc_lsa_SetSecret_r(b
, tctx
, &r3
),
86 torture_assert_ntstatus_ok(tctx
, r3
.out
.result
, "SetSecret failed");
88 r3
.in
.sec_handle
= &sec_handle
;
89 r3
.in
.new_val
= &buf1
;
91 r3
.in
.new_val
->data
= enc_key
.data
;
92 r3
.in
.new_val
->length
= enc_key
.length
;
93 r3
.in
.new_val
->size
= enc_key
.length
;
95 /* break the encrypted data */
98 torture_comment(tctx
, "Testing SetSecret with broken key\n");
100 torture_assert_ntstatus_ok(tctx
, dcerpc_lsa_SetSecret_r(b
, tctx
, &r3
),
102 torture_assert_ntstatus_equal(tctx
, r3
.out
.result
, NT_STATUS_UNKNOWN_REVISION
,
103 "SetSecret should have failed UNKNOWN_REVISION");
105 data_blob_free(&enc_key
);
107 ZERO_STRUCT(new_mtime
);
108 ZERO_STRUCT(old_mtime
);
110 /* fetch the secret back again */
111 r4
.in
.sec_handle
= &sec_handle
;
112 r4
.in
.new_val
= &bufp1
;
113 r4
.in
.new_mtime
= &new_mtime
;
114 r4
.in
.old_val
= NULL
;
115 r4
.in
.old_mtime
= NULL
;
119 torture_comment(tctx
, "Testing QuerySecret\n");
120 torture_assert_ntstatus_ok(tctx
, dcerpc_lsa_QuerySecret_r(b
, tctx
, &r4
),
121 "QuerySecret failed");
122 torture_assert_ntstatus_ok(tctx
, r4
.out
.result
, "QuerySecret failed");
123 if (r4
.out
.new_val
== NULL
|| r4
.out
.new_val
->buf
== NULL
)
124 torture_fail(tctx
, "No secret buffer returned");
125 blob1
.data
= r4
.out
.new_val
->buf
->data
;
126 blob1
.length
= r4
.out
.new_val
->buf
->size
;
128 blob2
= data_blob_talloc(tctx
, NULL
, blob1
.length
);
130 secret2
= sess_decrypt_string(tctx
, &blob1
, &session_key
);
132 torture_assert_str_equal(tctx
, secret1
, secret2
, "Returned secret invalid");
134 d
.in
.handle
= &sec_handle
;
135 d
.out
.handle
= &sec_handle
;
136 torture_assert_ntstatus_ok(tctx
, dcerpc_lsa_DeleteObject_r(b
, tctx
, &d
),
137 "DeleteObject failed");
138 torture_assert_ntstatus_ok(tctx
, d
.out
.result
, "delete should have returned OKINVALID_HANDLE");
142 struct secret_settings
{
143 uint32_t bindoptions
;
149 static bool test_secrets(struct torture_context
*torture
, const void *_data
)
151 struct dcerpc_pipe
*p
;
152 struct policy_handle
*handle
;
153 struct dcerpc_binding
*binding
;
154 const struct secret_settings
*settings
=
155 (const struct secret_settings
*)_data
;
157 struct dcerpc_binding_handle
*b
;
159 lpcfg_set_cmdline(torture
->lp_ctx
, "ntlmssp client:keyexchange", settings
->keyexchange
?"True":"False");
160 lpcfg_set_cmdline(torture
->lp_ctx
, "ntlmssp_client:ntlm2", settings
->ntlm2
?"True":"False");
161 lpcfg_set_cmdline(torture
->lp_ctx
, "ntlmssp_client:lm_key", settings
->lm_key
?"True":"False");
163 torture_assert_ntstatus_ok(torture
, torture_rpc_binding(torture
, &binding
),
164 "Getting bindoptions");
166 binding
->flags
|= settings
->bindoptions
;
168 status
= dcerpc_pipe_connect_b(torture
, &p
, binding
,
174 torture_assert_ntstatus_ok(torture
, status
, "connect");
175 b
= p
->binding_handle
;
177 if (!test_lsa_OpenPolicy2(b
, torture
, &handle
)) {
182 torture_assert(torture
, handle
, "OpenPolicy2 failed. This test cannot run against this server");
184 if (!test_CreateSecret_basic(p
, torture
, handle
)) {
194 static struct torture_tcase
*add_test(struct torture_suite
*suite
, uint32_t bindoptions
,
195 bool keyexchange
, bool ntlm2
, bool lm_key
)
198 struct secret_settings
*settings
;
200 settings
= talloc_zero(suite
, struct secret_settings
);
201 settings
->bindoptions
= bindoptions
;
203 if (bindoptions
== DCERPC_PUSH_BIGENDIAN
)
204 name
= talloc_strdup(suite
, "bigendian");
205 else if (bindoptions
== DCERPC_SEAL
)
206 name
= talloc_strdup(suite
, "seal");
207 else if (bindoptions
== 0)
208 name
= talloc_strdup(suite
, "none");
210 name
= talloc_strdup(suite
, "unknown");
212 name
= talloc_asprintf_append_buffer(name
, " keyexchange:%s", keyexchange
?"yes":"no");
213 settings
->keyexchange
= keyexchange
;
215 name
= talloc_asprintf_append_buffer(name
, " ntlm2:%s", ntlm2
?"yes":"no");
216 settings
->ntlm2
= ntlm2
;
218 name
= talloc_asprintf_append_buffer(name
, " lm_key:%s", lm_key
?"yes":"no");
219 settings
->lm_key
= lm_key
;
221 return torture_suite_add_simple_tcase_const(suite
, name
, test_secrets
,
225 static const bool bool_vals
[] = { true, false };
227 /* TEST session key correctness by pushing and pulling secrets */
228 struct torture_suite
*torture_rpc_lsa_secrets(TALLOC_CTX
*mem_ctx
)
230 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "lsa.secrets");
231 int keyexchange
, ntlm2
, lm_key
;
233 for (keyexchange
= 0; keyexchange
< ARRAY_SIZE(bool_vals
); keyexchange
++) {
234 for (ntlm2
= 0; ntlm2
< ARRAY_SIZE(bool_vals
); ntlm2
++) {
235 for (lm_key
= 0; lm_key
< ARRAY_SIZE(bool_vals
); lm_key
++) {
236 add_test(suite
, DCERPC_PUSH_BIGENDIAN
, bool_vals
[keyexchange
], bool_vals
[ntlm2
],
238 add_test(suite
, DCERPC_SEAL
, bool_vals
[keyexchange
], bool_vals
[ntlm2
], bool_vals
[lm_key
]);
239 add_test(suite
, 0, bool_vals
[keyexchange
], bool_vals
[ntlm2
], bool_vals
[lm_key
]);