s4-drs: Torture testcase for DS_REPL_INFO_METADATA_2_FOR_OBJ infoType of DsGetReplInfo()
[sfrench/samba-autobuild/.git] / source4 / torture / rpc / dsgetinfo.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    DsGetReplInfo test. Based on code from dssync.c
5
6    Copyright (C) Erick Nogueira do Nascimento 2009
7
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.
12
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.
17
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/>.
20 */
21
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"
35
36
37 struct DsGetinfoBindInfo {
38         struct dcerpc_pipe *drs_pipe;
39         struct drsuapi_DsBind req;
40         struct GUID bind_guid;
41         struct drsuapi_DsBindInfoCtr our_bind_info_ctr;
42         struct drsuapi_DsBindInfo28 our_bind_info28;
43         struct drsuapi_DsBindInfo28 peer_bind_info28;
44         struct policy_handle bind_handle;
45 };
46
47 struct DsGetinfoTest {
48         struct dcerpc_binding *drsuapi_binding;
49
50         const char *ldap_url;
51         const char *site_name;
52
53         const char *domain_dn;
54
55         /* what we need to do as 'Administrator' */
56         struct {
57                 struct cli_credentials *credentials;
58                 struct DsGetinfoBindInfo drsuapi;
59         } admin;
60 };
61
62
63
64 /*
65   return the default DN for a ldap server given a connected RPC pipe to the
66   server
67  */
68 static const char *torture_get_ldap_base_dn(struct torture_context *tctx, struct dcerpc_pipe *p)
69 {
70         const char *hostname = p->binding->target_hostname;
71         struct ldb_context *ldb;
72         const char *ldap_url = talloc_asprintf(p, "ldap://%s", hostname);
73         const char *attrs[] = { "defaultNamingContext", NULL };
74         const char *dnstr;
75         TALLOC_CTX *tmp_ctx = talloc_new(tctx);
76         int ret;
77         struct ldb_result *res;
78
79         ldb = ldb_init(tmp_ctx, tctx->ev);
80         if (ldb == NULL) {
81                 talloc_free(tmp_ctx);
82                 return NULL;
83         }
84
85         ret = ldb_connect(ldb, ldap_url, 0, NULL);
86         if (ret != LDB_SUCCESS) {
87                 torture_comment(tctx, "Failed to make LDB connection to target");
88                 talloc_free(tmp_ctx);
89                 return NULL;
90         }
91
92         ret = dsdb_search_dn(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, ""),
93                              attrs, 0);
94         if (ret != LDB_SUCCESS) {
95                 torture_comment(tctx, "Failed to get defaultNamingContext");
96                 talloc_free(tmp_ctx);
97                 return NULL;
98         }
99
100         dnstr = ldb_msg_find_attr_as_string(res->msgs[0], "defaultNamingContext", NULL);
101         dnstr = talloc_strdup(tctx, dnstr);
102         talloc_free(tmp_ctx);
103         return dnstr;
104 }
105
106
107 static struct DsGetinfoTest *test_create_context(struct torture_context *tctx)
108 {
109         NTSTATUS status;
110         struct DsGetinfoTest *ctx;
111         struct drsuapi_DsBindInfo28 *our_bind_info28;
112         struct drsuapi_DsBindInfoCtr *our_bind_info_ctr;
113         const char *binding = torture_setting_string(tctx, "binding", NULL);
114         ctx = talloc_zero(tctx, struct DsGetinfoTest);
115         if (!ctx) return NULL;
116
117         status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
118         if (!NT_STATUS_IS_OK(status)) {
119                 printf("Bad binding string %s\n", binding);
120                 return NULL;
121         }
122         ctx->drsuapi_binding->flags |= DCERPC_SIGN | DCERPC_SEAL;
123
124         /* ctx->admin ...*/
125         ctx->admin.credentials                          = cmdline_credentials;
126
127         our_bind_info28                         = &ctx->admin.drsuapi.our_bind_info28;
128         our_bind_info28->supported_extensions   = 0xFFFFFFFF;
129         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
130         our_bind_info28->site_guid              = GUID_zero();
131         our_bind_info28->pid                    = 0;
132         our_bind_info28->repl_epoch             = 1;
133
134         our_bind_info_ctr                       = &ctx->admin.drsuapi.our_bind_info_ctr;
135         our_bind_info_ctr->length               = 28;
136         our_bind_info_ctr->info.info28          = *our_bind_info28;
137
138         GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
139
140         ctx->admin.drsuapi.req.in.bind_guid             = &ctx->admin.drsuapi.bind_guid;
141         ctx->admin.drsuapi.req.in.bind_info             = our_bind_info_ctr;
142         ctx->admin.drsuapi.req.out.bind_handle          = &ctx->admin.drsuapi.bind_handle;
143
144         return ctx;
145 }
146
147 static bool _test_DsBind(struct torture_context *tctx,
148                          struct DsGetinfoTest *ctx, struct cli_credentials *credentials, struct DsGetinfoBindInfo *b)
149 {
150         NTSTATUS status;
151         bool ret = true;
152
153         status = dcerpc_pipe_connect_b(ctx,
154                                        &b->drs_pipe, ctx->drsuapi_binding,
155                                        &ndr_table_drsuapi,
156                                        credentials, tctx->ev, tctx->lp_ctx);
157
158         if (!NT_STATUS_IS_OK(status)) {
159                 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
160                 return false;
161         }
162
163         status = dcerpc_drsuapi_DsBind(b->drs_pipe, ctx, &b->req);
164         if (!NT_STATUS_IS_OK(status)) {
165                 const char *errstr = nt_errstr(status);
166                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
167                         errstr = dcerpc_errstr(ctx, b->drs_pipe->last_fault_code);
168                 }
169                 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
170                 ret = false;
171         } else if (!W_ERROR_IS_OK(b->req.out.result)) {
172                 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
173                 ret = false;
174         }
175
176         ZERO_STRUCT(b->peer_bind_info28);
177         if (b->req.out.bind_info) {
178                 switch (b->req.out.bind_info->length) {
179                 case 24: {
180                         struct drsuapi_DsBindInfo24 *info24;
181                         info24 = &b->req.out.bind_info->info.info24;
182                         b->peer_bind_info28.supported_extensions= info24->supported_extensions;
183                         b->peer_bind_info28.site_guid           = info24->site_guid;
184                         b->peer_bind_info28.pid                 = info24->pid;
185                         b->peer_bind_info28.repl_epoch          = 0;
186                         break;
187                 }
188                 case 48: {
189                         struct drsuapi_DsBindInfo48 *info48;
190                         info48 = &b->req.out.bind_info->info.info48;
191                         b->peer_bind_info28.supported_extensions= info48->supported_extensions;
192                         b->peer_bind_info28.site_guid           = info48->site_guid;
193                         b->peer_bind_info28.pid                 = info48->pid;
194                         b->peer_bind_info28.repl_epoch          = info48->repl_epoch;
195                         break;
196                 }
197                 case 28:
198                         b->peer_bind_info28 = b->req.out.bind_info->info.info28;
199                         break;
200                 default:
201                         printf("DsBind - warning: unknown BindInfo length: %u\n",
202                                b->req.out.bind_info->length);
203                 }
204         }
205
206         return ret;
207 }
208
209
210 static bool test_getinfo(struct torture_context *tctx,
211                          struct DsGetinfoTest *ctx)
212 {
213         NTSTATUS status;
214         struct dcerpc_pipe *p = ctx->admin.drsuapi.drs_pipe;
215         struct drsuapi_DsReplicaGetInfo r;
216         union drsuapi_DsReplicaGetInfoRequest req;
217         union drsuapi_DsReplicaInfo info;
218         enum drsuapi_DsReplicaInfoType info_type;
219         int i;
220         int invalid_levels = 0;
221         struct {
222                 int32_t level;
223                 int32_t infotype;
224                 const char *obj_dn;
225                 const char *attribute_name;
226                 uint32_t flags;
227         } array[] = {
228                 {
229                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
230                         .infotype = DRSUAPI_DS_REPLICA_INFO_NEIGHBORS
231                 },{
232                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
233                         .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS
234                 },{
235                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
236                         .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA
237                 },{
238                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
239                         .infotype = DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES
240                 },{
241                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
242                         .infotype = DRSUAPI_DS_REPLICA_INFO_KCC_DSA_LINK_FAILURES
243                 },{
244                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
245                         .infotype = DRSUAPI_DS_REPLICA_INFO_PENDING_OPS
246                 },{
247                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
248                         .infotype = DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA
249                 },{
250                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
251                         .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS2
252                 },{
253                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
254                         .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS3
255                 },{
256                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
257                         .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA2,
258                         .obj_dn = "CN=Domain Admins,CN=Users,",
259                         .flags = 0
260                 },{
261                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
262                         .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA2,
263                         .obj_dn = "CN=Domain Admins,CN=Users,",
264                         .flags = DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
265                 },{
266                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
267                         .infotype = DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA2
268                 },{
269                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
270                         .infotype = DRSUAPI_DS_REPLICA_INFO_REPSTO
271                 },{
272                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
273                         .infotype = DRSUAPI_DS_REPLICA_INFO_CLIENT_CONTEXTS
274                 },{
275                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
276                         .infotype = DRSUAPI_DS_REPLICA_INFO_UPTODATE_VECTOR_V1
277                 },{
278                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
279                         .infotype = DRSUAPI_DS_REPLICA_INFO_SERVER_OUTGOING_CALLS
280                 }
281         };
282
283         ctx->domain_dn = torture_get_ldap_base_dn(tctx, p);
284         torture_assert(tctx, ctx->domain_dn != NULL, "Cannot get domain_dn");
285
286         if (torture_setting_bool(tctx, "samba4", false)) {
287                 torture_comment(tctx, "skipping DsReplicaGetInfo test against Samba4\n");
288                 return true;
289         }
290
291         r.in.bind_handle        = &ctx->admin.drsuapi.bind_handle;
292         r.in.req                = &req;
293
294         for (i=0; i < ARRAY_SIZE(array); i++) {
295                 const char *object_dn;
296
297                 torture_comment(tctx, "testing DsReplicaGetInfo level %d infotype %d\n",
298                                 array[i].level, array[i].infotype);
299
300                 if (array[i].obj_dn) {
301                         object_dn = array[i].obj_dn;
302                         if (object_dn[strlen(object_dn)-1] == ',') {
303                                 /* add the domain DN on the end */
304                                 object_dn = talloc_asprintf(tctx, "%s%s", object_dn, ctx->domain_dn);
305                         }
306                 } else {
307                         object_dn = ctx->domain_dn;
308                 }
309
310                 r.in.level = array[i].level;
311                 switch(r.in.level) {
312                 case DRSUAPI_DS_REPLICA_GET_INFO:
313                         r.in.req->req1.info_type        = array[i].infotype;
314                         r.in.req->req1.object_dn        = object_dn;
315                         ZERO_STRUCT(r.in.req->req1.source_dsa_guid);
316                         break;
317                 case DRSUAPI_DS_REPLICA_GET_INFO2:
318                         r.in.req->req2.info_type        = array[i].infotype;
319                         r.in.req->req2.object_dn        = object_dn;
320                         ZERO_STRUCT(r.in.req->req2.source_dsa_guid);
321                         r.in.req->req2.flags            = 0;
322                         r.in.req->req2.attribute_name   = NULL;
323                         r.in.req->req2.value_dn_str     = NULL;
324                         r.in.req->req2.enumeration_context = 0;
325                         break;
326                 }
327
328                 /* Construct a different request for some of the infoTypes */
329                 if (array[i].attribute_name != NULL) {
330                         r.in.req->req2.attribute_name = array[i].attribute_name;
331                 }
332                 if (array[i].flags != 0) {
333                         r.in.req->req2.flags |= array[i].flags;
334                 }
335
336                 r.out.info              = &info;
337                 r.out.info_type         = &info_type;
338
339                 status = dcerpc_drsuapi_DsReplicaGetInfo(p, tctx, &r);
340
341                 if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_LEVEL)) {
342                         /* this is a not yet supported level */
343                         torture_comment(tctx,
344                                         "DsReplicaGetInfo level %d and/or infotype %d not yet supported by server\n",
345                                         array[i].level, array[i].infotype);
346                         invalid_levels++;
347                 } else if (!NT_STATUS_IS_OK(status) && p->last_fault_code == DCERPC_FAULT_INVALID_TAG) {
348                         torture_comment(tctx,
349                                         "DsReplicaGetInfo level %d and/or infotype %d not supported by server\n",
350                                         array[i].level, array[i].infotype);
351                 }/* else {
352                         torture_drsuapi_assert_call(tctx, p, status, &r, "dcerpc_drsuapi_DsReplicaGetInfo");
353                 }*/
354         }
355
356         if (invalid_levels > 0) {
357                 return false;
358         }
359
360         return true;
361 }
362
363 /**
364  * DSGETINFO test case setup
365  */
366 static bool torture_dsgetinfo_tcase_setup(struct torture_context *tctx, void **data)
367 {
368         bool bret;
369         struct DsGetinfoTest *ctx;
370
371         *data = ctx = test_create_context(tctx);
372         torture_assert(tctx, ctx, "test_create_context() failed");
373
374         bret = _test_DsBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
375         torture_assert(tctx, bret, "_test_DsBind() failed");
376
377         return true;
378 }
379
380 /**
381  * DSGETINFO test case cleanup
382  */
383 static bool torture_dsgetinfo_tcase_teardown(struct torture_context *tctx, void *data)
384 {
385         struct DsGetinfoTest *ctx;
386         struct drsuapi_DsUnbind r;
387         struct policy_handle bind_handle;
388
389         ctx = talloc_get_type(data, struct DsGetinfoTest);
390
391         ZERO_STRUCT(r);
392         r.out.bind_handle = &bind_handle;
393
394         /* Unbing admin handle */
395         r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
396         dcerpc_drsuapi_DsUnbind(ctx->admin.drsuapi.drs_pipe, ctx, &r);
397
398         talloc_free(ctx);
399
400         return true;
401 }
402
403 /**
404  * DSGETINFO test case implementation
405  */
406 void torture_drs_rpc_dsgetinfo_tcase(struct torture_suite *suite)
407 {
408         typedef bool (*run_func) (struct torture_context *test, void *tcase_data);
409
410         struct torture_test *test;
411         struct torture_tcase *tcase = torture_suite_add_tcase(suite, "DSGETINFO");
412
413         torture_tcase_set_fixture(tcase,
414                                   torture_dsgetinfo_tcase_setup,
415                                   torture_dsgetinfo_tcase_teardown);
416
417         test = torture_tcase_add_simple_test(tcase, "DsGetReplicaInfo", (run_func)test_getinfo);
418 }
419