python:tests: Use assertMultiLineEqual() to get better failure output
[Samba.git] / source4 / torture / rpc / dsgetinfo.c
blobb47d6ee9e5348bc6ce15b2dc2f37f06bc87ad100
1 /*
2 Unix SMB/CIFS implementation.
4 DsGetReplInfo test. Based on code from dssync.c
6 Copyright (C) Erick Nogueira do Nascimento 2009
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/>.
22 #include "includes.h"
23 #include "lib/cmdline/cmdline.h"
24 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
25 #include "librpc/gen_ndr/ndr_drsblobs.h"
26 #include "libcli/cldap/cldap.h"
27 #include "torture/torture.h"
28 #include "../libcli/drsuapi/drsuapi.h"
29 #include "auth/gensec/gensec.h"
30 #include "param/param.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "torture/rpc/torture_rpc.h"
33 #include "torture/drs/proto.h"
34 #include "lib/util/util_paths.h"
37 struct DsGetinfoBindInfo {
38 struct dcerpc_pipe *drs_pipe;
39 struct dcerpc_binding_handle *drs_handle;
40 struct drsuapi_DsBind req;
41 struct GUID bind_guid;
42 struct drsuapi_DsBindInfoCtr our_bind_info_ctr;
43 struct drsuapi_DsBindInfo28 our_bind_info28;
44 struct drsuapi_DsBindInfo28 peer_bind_info28;
45 struct policy_handle bind_handle;
48 struct DsGetinfoTest {
49 struct dcerpc_binding *drsuapi_binding;
51 const char *ldap_url;
52 const char *site_name;
54 const char *domain_dn;
56 /* what we need to do as 'Administrator' */
57 struct {
58 struct cli_credentials *credentials;
59 struct DsGetinfoBindInfo drsuapi;
60 } admin;
66 return the default DN for a ldap server given a connected RPC pipe to the
67 server
69 static const char *torture_get_ldap_base_dn(struct torture_context *tctx, struct dcerpc_pipe *p)
71 const char *hostname = dcerpc_binding_get_string_option(p->binding, "host");
72 struct ldb_context *ldb;
73 const char *ldap_url = talloc_asprintf(p, "ldap://%s", hostname);
74 const char *attrs[] = { "defaultNamingContext", NULL };
75 const char *dnstr;
76 TALLOC_CTX *tmp_ctx = talloc_new(tctx);
77 int ret;
78 struct ldb_result *res;
80 ldb = ldb_init(tmp_ctx, tctx->ev);
81 if (ldb == NULL) {
82 talloc_free(tmp_ctx);
83 return NULL;
86 if (ldb_set_opaque(ldb, "loadparm", tctx->lp_ctx)) {
87 talloc_free(ldb);
88 return NULL;
91 ldb_set_modules_dir(ldb,
92 modules_path(ldb, "ldb"));
94 ret = ldb_connect(ldb, ldap_url, 0, NULL);
95 if (ret != LDB_SUCCESS) {
96 torture_comment(tctx, "Failed to make LDB connection to target");
97 talloc_free(tmp_ctx);
98 return NULL;
101 ret = dsdb_search_dn(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, ""),
102 attrs, 0);
103 if (ret != LDB_SUCCESS) {
104 torture_comment(tctx, "Failed to get defaultNamingContext");
105 talloc_free(tmp_ctx);
106 return NULL;
109 dnstr = ldb_msg_find_attr_as_string(res->msgs[0], "defaultNamingContext", NULL);
110 dnstr = talloc_strdup(tctx, dnstr);
111 talloc_free(tmp_ctx);
112 return dnstr;
116 static struct DsGetinfoTest *test_create_context(struct torture_context *tctx)
118 NTSTATUS status;
119 struct DsGetinfoTest *ctx;
120 struct drsuapi_DsBindInfo28 *our_bind_info28;
121 struct drsuapi_DsBindInfoCtr *our_bind_info_ctr;
122 const char *binding = torture_setting_string(tctx, "binding", NULL);
123 ctx = talloc_zero(tctx, struct DsGetinfoTest);
124 if (!ctx) return NULL;
126 status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
127 if (!NT_STATUS_IS_OK(status)) {
128 printf("Bad binding string %s\n", binding);
129 return NULL;
131 status = dcerpc_binding_set_flags(ctx->drsuapi_binding, DCERPC_SIGN | DCERPC_SEAL, 0);
132 if (!NT_STATUS_IS_OK(status)) {
133 printf("dcerpc_binding_set_flags - %s\n", nt_errstr(status));
134 return NULL;
137 /* ctx->admin ...*/
138 ctx->admin.credentials = samba_cmdline_get_creds();
140 our_bind_info28 = &ctx->admin.drsuapi.our_bind_info28;
141 our_bind_info28->supported_extensions = 0xFFFFFFFF;
142 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
143 our_bind_info28->site_guid = GUID_zero();
144 our_bind_info28->pid = 0;
145 our_bind_info28->repl_epoch = 1;
147 our_bind_info_ctr = &ctx->admin.drsuapi.our_bind_info_ctr;
148 our_bind_info_ctr->length = 28;
149 our_bind_info_ctr->info.info28 = *our_bind_info28;
151 GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
153 ctx->admin.drsuapi.req.in.bind_guid = &ctx->admin.drsuapi.bind_guid;
154 ctx->admin.drsuapi.req.in.bind_info = our_bind_info_ctr;
155 ctx->admin.drsuapi.req.out.bind_handle = &ctx->admin.drsuapi.bind_handle;
157 return ctx;
160 static bool _test_DsBind(struct torture_context *tctx,
161 struct DsGetinfoTest *ctx, struct cli_credentials *credentials, struct DsGetinfoBindInfo *b)
163 NTSTATUS status;
164 bool ret = true;
166 status = dcerpc_pipe_connect_b(ctx,
167 &b->drs_pipe, ctx->drsuapi_binding,
168 &ndr_table_drsuapi,
169 credentials, tctx->ev, tctx->lp_ctx);
171 if (!NT_STATUS_IS_OK(status)) {
172 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
173 return false;
175 b->drs_handle = b->drs_pipe->binding_handle;
177 status = dcerpc_drsuapi_DsBind_r(b->drs_handle, ctx, &b->req);
178 if (!NT_STATUS_IS_OK(status)) {
179 const char *errstr = nt_errstr(status);
180 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
181 ret = false;
182 } else if (!W_ERROR_IS_OK(b->req.out.result)) {
183 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
184 ret = false;
187 ZERO_STRUCT(b->peer_bind_info28);
188 if (b->req.out.bind_info) {
189 switch (b->req.out.bind_info->length) {
190 case 24: {
191 struct drsuapi_DsBindInfo24 *info24;
192 info24 = &b->req.out.bind_info->info.info24;
193 b->peer_bind_info28.supported_extensions= info24->supported_extensions;
194 b->peer_bind_info28.site_guid = info24->site_guid;
195 b->peer_bind_info28.pid = info24->pid;
196 b->peer_bind_info28.repl_epoch = 0;
197 break;
199 case 28: {
200 b->peer_bind_info28 = b->req.out.bind_info->info.info28;
201 break;
203 case 32: {
204 struct drsuapi_DsBindInfo32 *info32;
205 info32 = &b->req.out.bind_info->info.info32;
206 b->peer_bind_info28.supported_extensions= info32->supported_extensions;
207 b->peer_bind_info28.site_guid = info32->site_guid;
208 b->peer_bind_info28.pid = info32->pid;
209 b->peer_bind_info28.repl_epoch = info32->repl_epoch;
210 break;
212 case 48: {
213 struct drsuapi_DsBindInfo48 *info48;
214 info48 = &b->req.out.bind_info->info.info48;
215 b->peer_bind_info28.supported_extensions= info48->supported_extensions;
216 b->peer_bind_info28.site_guid = info48->site_guid;
217 b->peer_bind_info28.pid = info48->pid;
218 b->peer_bind_info28.repl_epoch = info48->repl_epoch;
219 break;
221 case 52: {
222 struct drsuapi_DsBindInfo52 *info52;
223 info52 = &b->req.out.bind_info->info.info52;
224 b->peer_bind_info28.supported_extensions= info52->supported_extensions;
225 b->peer_bind_info28.site_guid = info52->site_guid;
226 b->peer_bind_info28.pid = info52->pid;
227 b->peer_bind_info28.repl_epoch = info52->repl_epoch;
228 break;
230 default:
231 printf("DsBind - warning: unknown BindInfo length: %u\n",
232 b->req.out.bind_info->length);
236 return ret;
240 static bool test_getinfo(struct torture_context *tctx,
241 struct DsGetinfoTest *ctx)
243 NTSTATUS status;
244 struct dcerpc_pipe *p = ctx->admin.drsuapi.drs_pipe;
245 struct dcerpc_binding_handle *b = ctx->admin.drsuapi.drs_handle;
246 struct drsuapi_DsReplicaGetInfo r;
247 union drsuapi_DsReplicaGetInfoRequest req;
248 union drsuapi_DsReplicaInfo info;
249 enum drsuapi_DsReplicaInfoType info_type;
250 int i;
251 bool no_invalid_levels = true;
252 struct {
253 int32_t level;
254 int32_t infotype;
255 const char *obj_dn;
256 const char *attribute_name;
257 uint32_t flags;
258 } array[] = {
260 .level = DRSUAPI_DS_REPLICA_GET_INFO,
261 .infotype = DRSUAPI_DS_REPLICA_INFO_NEIGHBORS
263 .level = DRSUAPI_DS_REPLICA_GET_INFO,
264 .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS
266 .level = DRSUAPI_DS_REPLICA_GET_INFO,
267 .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA
269 .level = DRSUAPI_DS_REPLICA_GET_INFO,
270 .infotype = DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES
272 .level = DRSUAPI_DS_REPLICA_GET_INFO,
273 .infotype = DRSUAPI_DS_REPLICA_INFO_KCC_DSA_LINK_FAILURES
275 .level = DRSUAPI_DS_REPLICA_GET_INFO,
276 .infotype = DRSUAPI_DS_REPLICA_INFO_PENDING_OPS
278 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
279 .infotype = DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA
281 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
282 .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS2
284 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
285 .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS3
287 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
288 .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA2,
289 .obj_dn = "CN=Domain Admins,CN=Users,",
290 .flags = 0
292 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
293 .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA2,
294 .obj_dn = "CN=Domain Admins,CN=Users,",
295 .flags = DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
297 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
298 .infotype = DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA2
300 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
301 .infotype = DRSUAPI_DS_REPLICA_INFO_REPSTO
303 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
304 .infotype = DRSUAPI_DS_REPLICA_INFO_CLIENT_CONTEXTS
306 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
307 .infotype = DRSUAPI_DS_REPLICA_INFO_UPTODATE_VECTOR_V1
309 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
310 .infotype = DRSUAPI_DS_REPLICA_INFO_SERVER_OUTGOING_CALLS
314 ctx->domain_dn = torture_get_ldap_base_dn(tctx, p);
315 torture_assert(tctx, ctx->domain_dn != NULL, "Cannot get domain_dn");
317 if (torture_setting_bool(tctx, "samba4", false)) {
318 torture_comment(tctx, "skipping DsReplicaGetInfo test against Samba4\n");
319 return true;
322 r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
323 r.in.req = &req;
325 for (i=0; i < ARRAY_SIZE(array); i++) {
326 const char *object_dn;
328 torture_comment(tctx, "Testing DsReplicaGetInfo level %d infotype %d\n",
329 array[i].level, array[i].infotype);
331 if (array[i].obj_dn) {
332 object_dn = array[i].obj_dn;
333 if (object_dn[strlen(object_dn)-1] == ',') {
334 /* add the domain DN on the end */
335 object_dn = talloc_asprintf(tctx, "%s%s", object_dn, ctx->domain_dn);
337 } else {
338 object_dn = ctx->domain_dn;
341 r.in.level = array[i].level;
342 switch(r.in.level) {
343 case DRSUAPI_DS_REPLICA_GET_INFO:
344 r.in.req->req1.info_type = array[i].infotype;
345 r.in.req->req1.object_dn = object_dn;
346 ZERO_STRUCT(r.in.req->req1.source_dsa_guid);
347 break;
348 case DRSUAPI_DS_REPLICA_GET_INFO2:
349 r.in.req->req2.info_type = array[i].infotype;
350 r.in.req->req2.object_dn = object_dn;
351 ZERO_STRUCT(r.in.req->req2.source_dsa_guid);
352 r.in.req->req2.flags = 0;
353 r.in.req->req2.attribute_name = NULL;
354 r.in.req->req2.value_dn_str = NULL;
355 r.in.req->req2.enumeration_context = 0;
356 break;
359 /* Construct a different request for some of the infoTypes */
360 if (array[i].attribute_name != NULL) {
361 r.in.req->req2.attribute_name = array[i].attribute_name;
363 if (array[i].flags != 0) {
364 r.in.req->req2.flags |= array[i].flags;
367 r.out.info = &info;
368 r.out.info_type = &info_type;
370 status = dcerpc_drsuapi_DsReplicaGetInfo_r(b, tctx, &r);
371 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
372 torture_comment(tctx,
373 "DsReplicaGetInfo level %d and/or infotype %d not supported by server\n",
374 array[i].level, array[i].infotype);
375 continue;
377 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx,
378 "DsReplicaGetInfo level %d and/or infotype %d failed\n",
379 array[i].level, array[i].infotype));
380 if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_LEVEL)) {
381 /* this is a not yet supported level */
382 torture_comment(tctx,
383 "DsReplicaGetInfo level %d and/or infotype %d not yet supported by server\n",
384 array[i].level, array[i].infotype);
385 no_invalid_levels = false;
386 continue;
389 torture_drsuapi_assert_call(tctx, p, status, &r, "dcerpc_drsuapi_DsReplicaGetInfo");
392 return no_invalid_levels;
396 * DSGETINFO test case setup
398 static bool torture_dsgetinfo_tcase_setup(struct torture_context *tctx, void **data)
400 bool bret;
401 struct DsGetinfoTest *ctx;
403 *data = ctx = test_create_context(tctx);
404 torture_assert(tctx, ctx, "test_create_context() failed");
406 bret = _test_DsBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
407 torture_assert(tctx, bret, "_test_DsBind() failed");
409 return true;
413 * DSGETINFO test case cleanup
415 static bool torture_dsgetinfo_tcase_teardown(struct torture_context *tctx, void *data)
417 struct DsGetinfoTest *ctx;
418 struct drsuapi_DsUnbind r;
419 struct policy_handle bind_handle;
421 ctx = talloc_get_type(data, struct DsGetinfoTest);
423 ZERO_STRUCT(r);
424 r.out.bind_handle = &bind_handle;
426 /* Unbing admin handle */
427 r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
428 if (ctx->admin.drsuapi.drs_handle) {
429 dcerpc_drsuapi_DsUnbind_r(ctx->admin.drsuapi.drs_handle,
430 ctx, &r);
433 talloc_free(ctx);
435 return true;
439 * DSGETINFO test case implementation
441 void torture_drs_rpc_dsgetinfo_tcase(struct torture_suite *suite)
443 typedef bool (*run_func) (struct torture_context *test, void *tcase_data);
444 struct torture_tcase *tcase = torture_suite_add_tcase(suite, "dsgetinfo");
446 torture_tcase_set_fixture(tcase,
447 torture_dsgetinfo_tcase_setup,
448 torture_dsgetinfo_tcase_teardown);
450 torture_tcase_add_simple_test(tcase, "DsGetReplicaInfo", (run_func)test_getinfo);