subunit: Support formatting compatible with upstream subunit, for consistency.
[Samba/gebeck_regimport.git] / source4 / torture / rpc / dsgetinfo.c
blobb01dd773a666a750229e638a51a4a4a7031247f4
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/popt_common.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 "lib/ldb_wrap.h"
33 #include "torture/rpc/rpc.h"
34 #include "torture/drs/proto.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 = 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 ret = ldb_connect(ldb, ldap_url, 0, NULL);
92 if (ret != LDB_SUCCESS) {
93 torture_comment(tctx, "Failed to make LDB connection to target");
94 talloc_free(tmp_ctx);
95 return NULL;
98 ret = dsdb_search_dn(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, ""),
99 attrs, 0);
100 if (ret != LDB_SUCCESS) {
101 torture_comment(tctx, "Failed to get defaultNamingContext");
102 talloc_free(tmp_ctx);
103 return NULL;
106 dnstr = ldb_msg_find_attr_as_string(res->msgs[0], "defaultNamingContext", NULL);
107 dnstr = talloc_strdup(tctx, dnstr);
108 talloc_free(tmp_ctx);
109 return dnstr;
113 static struct DsGetinfoTest *test_create_context(struct torture_context *tctx)
115 NTSTATUS status;
116 struct DsGetinfoTest *ctx;
117 struct drsuapi_DsBindInfo28 *our_bind_info28;
118 struct drsuapi_DsBindInfoCtr *our_bind_info_ctr;
119 const char *binding = torture_setting_string(tctx, "binding", NULL);
120 ctx = talloc_zero(tctx, struct DsGetinfoTest);
121 if (!ctx) return NULL;
123 status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
124 if (!NT_STATUS_IS_OK(status)) {
125 printf("Bad binding string %s\n", binding);
126 return NULL;
128 ctx->drsuapi_binding->flags |= DCERPC_SIGN | DCERPC_SEAL;
130 /* ctx->admin ...*/
131 ctx->admin.credentials = cmdline_credentials;
133 our_bind_info28 = &ctx->admin.drsuapi.our_bind_info28;
134 our_bind_info28->supported_extensions = 0xFFFFFFFF;
135 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
136 our_bind_info28->site_guid = GUID_zero();
137 our_bind_info28->pid = 0;
138 our_bind_info28->repl_epoch = 1;
140 our_bind_info_ctr = &ctx->admin.drsuapi.our_bind_info_ctr;
141 our_bind_info_ctr->length = 28;
142 our_bind_info_ctr->info.info28 = *our_bind_info28;
144 GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
146 ctx->admin.drsuapi.req.in.bind_guid = &ctx->admin.drsuapi.bind_guid;
147 ctx->admin.drsuapi.req.in.bind_info = our_bind_info_ctr;
148 ctx->admin.drsuapi.req.out.bind_handle = &ctx->admin.drsuapi.bind_handle;
150 return ctx;
153 static bool _test_DsBind(struct torture_context *tctx,
154 struct DsGetinfoTest *ctx, struct cli_credentials *credentials, struct DsGetinfoBindInfo *b)
156 NTSTATUS status;
157 bool ret = true;
159 status = dcerpc_pipe_connect_b(ctx,
160 &b->drs_pipe, ctx->drsuapi_binding,
161 &ndr_table_drsuapi,
162 credentials, tctx->ev, tctx->lp_ctx);
164 if (!NT_STATUS_IS_OK(status)) {
165 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
166 return false;
168 b->drs_handle = b->drs_pipe->binding_handle;
170 status = dcerpc_drsuapi_DsBind_r(b->drs_handle, ctx, &b->req);
171 if (!NT_STATUS_IS_OK(status)) {
172 const char *errstr = nt_errstr(status);
173 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
174 errstr = dcerpc_errstr(ctx, b->drs_pipe->last_fault_code);
176 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
177 ret = false;
178 } else if (!W_ERROR_IS_OK(b->req.out.result)) {
179 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
180 ret = false;
183 ZERO_STRUCT(b->peer_bind_info28);
184 if (b->req.out.bind_info) {
185 switch (b->req.out.bind_info->length) {
186 case 24: {
187 struct drsuapi_DsBindInfo24 *info24;
188 info24 = &b->req.out.bind_info->info.info24;
189 b->peer_bind_info28.supported_extensions= info24->supported_extensions;
190 b->peer_bind_info28.site_guid = info24->site_guid;
191 b->peer_bind_info28.pid = info24->pid;
192 b->peer_bind_info28.repl_epoch = 0;
193 break;
195 case 48: {
196 struct drsuapi_DsBindInfo48 *info48;
197 info48 = &b->req.out.bind_info->info.info48;
198 b->peer_bind_info28.supported_extensions= info48->supported_extensions;
199 b->peer_bind_info28.site_guid = info48->site_guid;
200 b->peer_bind_info28.pid = info48->pid;
201 b->peer_bind_info28.repl_epoch = info48->repl_epoch;
202 break;
204 case 28:
205 b->peer_bind_info28 = b->req.out.bind_info->info.info28;
206 break;
207 default:
208 printf("DsBind - warning: unknown BindInfo length: %u\n",
209 b->req.out.bind_info->length);
213 return ret;
217 static bool test_getinfo(struct torture_context *tctx,
218 struct DsGetinfoTest *ctx)
220 NTSTATUS status;
221 struct dcerpc_pipe *p = ctx->admin.drsuapi.drs_pipe;
222 struct dcerpc_binding_handle *b = ctx->admin.drsuapi.drs_handle;
223 struct drsuapi_DsReplicaGetInfo r;
224 union drsuapi_DsReplicaGetInfoRequest req;
225 union drsuapi_DsReplicaInfo info;
226 enum drsuapi_DsReplicaInfoType info_type;
227 int i;
228 int invalid_levels = 0;
229 struct {
230 int32_t level;
231 int32_t infotype;
232 const char *obj_dn;
233 const char *attribute_name;
234 uint32_t flags;
235 } array[] = {
237 .level = DRSUAPI_DS_REPLICA_GET_INFO,
238 .infotype = DRSUAPI_DS_REPLICA_INFO_NEIGHBORS
240 .level = DRSUAPI_DS_REPLICA_GET_INFO,
241 .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS
243 .level = DRSUAPI_DS_REPLICA_GET_INFO,
244 .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA
246 .level = DRSUAPI_DS_REPLICA_GET_INFO,
247 .infotype = DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES
249 .level = DRSUAPI_DS_REPLICA_GET_INFO,
250 .infotype = DRSUAPI_DS_REPLICA_INFO_KCC_DSA_LINK_FAILURES
252 .level = DRSUAPI_DS_REPLICA_GET_INFO,
253 .infotype = DRSUAPI_DS_REPLICA_INFO_PENDING_OPS
255 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
256 .infotype = DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA
258 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
259 .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS2
261 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
262 .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS3
264 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
265 .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA2,
266 .obj_dn = "CN=Domain Admins,CN=Users,",
267 .flags = 0
269 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
270 .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA2,
271 .obj_dn = "CN=Domain Admins,CN=Users,",
272 .flags = DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
274 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
275 .infotype = DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA2
277 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
278 .infotype = DRSUAPI_DS_REPLICA_INFO_REPSTO
280 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
281 .infotype = DRSUAPI_DS_REPLICA_INFO_CLIENT_CONTEXTS
283 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
284 .infotype = DRSUAPI_DS_REPLICA_INFO_UPTODATE_VECTOR_V1
286 .level = DRSUAPI_DS_REPLICA_GET_INFO2,
287 .infotype = DRSUAPI_DS_REPLICA_INFO_SERVER_OUTGOING_CALLS
291 ctx->domain_dn = torture_get_ldap_base_dn(tctx, p);
292 torture_assert(tctx, ctx->domain_dn != NULL, "Cannot get domain_dn");
294 if (torture_setting_bool(tctx, "samba4", false)) {
295 torture_comment(tctx, "skipping DsReplicaGetInfo test against Samba4\n");
296 return true;
299 r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
300 r.in.req = &req;
302 for (i=0; i < ARRAY_SIZE(array); i++) {
303 const char *object_dn;
305 torture_comment(tctx, "Testing DsReplicaGetInfo level %d infotype %d\n",
306 array[i].level, array[i].infotype);
308 if (array[i].obj_dn) {
309 object_dn = array[i].obj_dn;
310 if (object_dn[strlen(object_dn)-1] == ',') {
311 /* add the domain DN on the end */
312 object_dn = talloc_asprintf(tctx, "%s%s", object_dn, ctx->domain_dn);
314 } else {
315 object_dn = ctx->domain_dn;
318 r.in.level = array[i].level;
319 switch(r.in.level) {
320 case DRSUAPI_DS_REPLICA_GET_INFO:
321 r.in.req->req1.info_type = array[i].infotype;
322 r.in.req->req1.object_dn = object_dn;
323 ZERO_STRUCT(r.in.req->req1.source_dsa_guid);
324 break;
325 case DRSUAPI_DS_REPLICA_GET_INFO2:
326 r.in.req->req2.info_type = array[i].infotype;
327 r.in.req->req2.object_dn = object_dn;
328 ZERO_STRUCT(r.in.req->req2.source_dsa_guid);
329 r.in.req->req2.flags = 0;
330 r.in.req->req2.attribute_name = NULL;
331 r.in.req->req2.value_dn_str = NULL;
332 r.in.req->req2.enumeration_context = 0;
333 break;
336 /* Construct a different request for some of the infoTypes */
337 if (array[i].attribute_name != NULL) {
338 r.in.req->req2.attribute_name = array[i].attribute_name;
340 if (array[i].flags != 0) {
341 r.in.req->req2.flags |= array[i].flags;
344 r.out.info = &info;
345 r.out.info_type = &info_type;
347 status = dcerpc_drsuapi_DsReplicaGetInfo_r(b, tctx, &r);
349 if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_LEVEL)) {
350 /* this is a not yet supported level */
351 torture_comment(tctx,
352 "DsReplicaGetInfo level %d and/or infotype %d not yet supported by server\n",
353 array[i].level, array[i].infotype);
354 invalid_levels++;
355 } else if (!NT_STATUS_IS_OK(status) && p->last_fault_code == DCERPC_FAULT_INVALID_TAG) {
356 torture_comment(tctx,
357 "DsReplicaGetInfo level %d and/or infotype %d not supported by server\n",
358 array[i].level, array[i].infotype);
359 }/* else {
360 torture_drsuapi_assert_call(tctx, p, status, &r, "dcerpc_drsuapi_DsReplicaGetInfo");
364 if (invalid_levels > 0) {
365 return false;
368 return true;
372 * DSGETINFO test case setup
374 static bool torture_dsgetinfo_tcase_setup(struct torture_context *tctx, void **data)
376 bool bret;
377 struct DsGetinfoTest *ctx;
379 *data = ctx = test_create_context(tctx);
380 torture_assert(tctx, ctx, "test_create_context() failed");
382 bret = _test_DsBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
383 torture_assert(tctx, bret, "_test_DsBind() failed");
385 return true;
389 * DSGETINFO test case cleanup
391 static bool torture_dsgetinfo_tcase_teardown(struct torture_context *tctx, void *data)
393 struct DsGetinfoTest *ctx;
394 struct drsuapi_DsUnbind r;
395 struct policy_handle bind_handle;
397 ctx = talloc_get_type(data, struct DsGetinfoTest);
399 ZERO_STRUCT(r);
400 r.out.bind_handle = &bind_handle;
402 /* Unbing admin handle */
403 r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
404 dcerpc_drsuapi_DsUnbind_r(ctx->admin.drsuapi.drs_handle, ctx, &r);
406 talloc_free(ctx);
408 return true;
412 * DSGETINFO test case implementation
414 void torture_drs_rpc_dsgetinfo_tcase(struct torture_suite *suite)
416 typedef bool (*run_func) (struct torture_context *test, void *tcase_data);
418 struct torture_test *test;
419 struct torture_tcase *tcase = torture_suite_add_tcase(suite, "DSGETINFO");
421 torture_tcase_set_fixture(tcase,
422 torture_dsgetinfo_tcase_setup,
423 torture_dsgetinfo_tcase_teardown);
425 test = torture_tcase_add_simple_test(tcase, "DsGetReplicaInfo", (run_func)test_getinfo);