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