s4: popt: Global replace of cmdline_credentials -> popt_get_cmdline_credentials().
[gd/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 = dcerpc_binding_get_string_option(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         status = dcerpc_binding_set_flags(ctx->drsuapi_binding, DCERPC_SIGN | DCERPC_SEAL, 0);
131         if (!NT_STATUS_IS_OK(status)) {
132                 printf("dcerpc_binding_set_flags - %s\n", nt_errstr(status));
133                 return NULL;
134         }
135
136         /* ctx->admin ...*/
137         ctx->admin.credentials = popt_get_cmdline_credentials();
138
139         our_bind_info28                         = &ctx->admin.drsuapi.our_bind_info28;
140         our_bind_info28->supported_extensions   = 0xFFFFFFFF;
141         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
142         our_bind_info28->site_guid              = GUID_zero();
143         our_bind_info28->pid                    = 0;
144         our_bind_info28->repl_epoch             = 1;
145
146         our_bind_info_ctr                       = &ctx->admin.drsuapi.our_bind_info_ctr;
147         our_bind_info_ctr->length               = 28;
148         our_bind_info_ctr->info.info28          = *our_bind_info28;
149
150         GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
151
152         ctx->admin.drsuapi.req.in.bind_guid             = &ctx->admin.drsuapi.bind_guid;
153         ctx->admin.drsuapi.req.in.bind_info             = our_bind_info_ctr;
154         ctx->admin.drsuapi.req.out.bind_handle          = &ctx->admin.drsuapi.bind_handle;
155
156         return ctx;
157 }
158
159 static bool _test_DsBind(struct torture_context *tctx,
160                          struct DsGetinfoTest *ctx, struct cli_credentials *credentials, struct DsGetinfoBindInfo *b)
161 {
162         NTSTATUS status;
163         bool ret = true;
164
165         status = dcerpc_pipe_connect_b(ctx,
166                                        &b->drs_pipe, ctx->drsuapi_binding,
167                                        &ndr_table_drsuapi,
168                                        credentials, tctx->ev, tctx->lp_ctx);
169
170         if (!NT_STATUS_IS_OK(status)) {
171                 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
172                 return false;
173         }
174         b->drs_handle = b->drs_pipe->binding_handle;
175
176         status = dcerpc_drsuapi_DsBind_r(b->drs_handle, ctx, &b->req);
177         if (!NT_STATUS_IS_OK(status)) {
178                 const char *errstr = nt_errstr(status);
179                 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
180                 ret = false;
181         } else if (!W_ERROR_IS_OK(b->req.out.result)) {
182                 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
183                 ret = false;
184         }
185
186         ZERO_STRUCT(b->peer_bind_info28);
187         if (b->req.out.bind_info) {
188                 switch (b->req.out.bind_info->length) {
189                 case 24: {
190                         struct drsuapi_DsBindInfo24 *info24;
191                         info24 = &b->req.out.bind_info->info.info24;
192                         b->peer_bind_info28.supported_extensions= info24->supported_extensions;
193                         b->peer_bind_info28.site_guid           = info24->site_guid;
194                         b->peer_bind_info28.pid                 = info24->pid;
195                         b->peer_bind_info28.repl_epoch          = 0;
196                         break;
197                 }
198                 case 28: {
199                         b->peer_bind_info28 = b->req.out.bind_info->info.info28;
200                         break;
201                 }
202                 case 32: {
203                         struct drsuapi_DsBindInfo32 *info32;
204                         info32 = &b->req.out.bind_info->info.info32;
205                         b->peer_bind_info28.supported_extensions= info32->supported_extensions;
206                         b->peer_bind_info28.site_guid           = info32->site_guid;
207                         b->peer_bind_info28.pid                 = info32->pid;
208                         b->peer_bind_info28.repl_epoch          = info32->repl_epoch;
209                         break;
210                 }
211                 case 48: {
212                         struct drsuapi_DsBindInfo48 *info48;
213                         info48 = &b->req.out.bind_info->info.info48;
214                         b->peer_bind_info28.supported_extensions= info48->supported_extensions;
215                         b->peer_bind_info28.site_guid           = info48->site_guid;
216                         b->peer_bind_info28.pid                 = info48->pid;
217                         b->peer_bind_info28.repl_epoch          = info48->repl_epoch;
218                         break;
219                 }
220                 case 52: {
221                         struct drsuapi_DsBindInfo52 *info52;
222                         info52 = &b->req.out.bind_info->info.info52;
223                         b->peer_bind_info28.supported_extensions= info52->supported_extensions;
224                         b->peer_bind_info28.site_guid           = info52->site_guid;
225                         b->peer_bind_info28.pid                 = info52->pid;
226                         b->peer_bind_info28.repl_epoch          = info52->repl_epoch;
227                         break;
228                 }
229                 default:
230                         printf("DsBind - warning: unknown BindInfo length: %u\n",
231                                b->req.out.bind_info->length);
232                 }
233         }
234
235         return ret;
236 }
237
238
239 static bool test_getinfo(struct torture_context *tctx,
240                          struct DsGetinfoTest *ctx)
241 {
242         NTSTATUS status;
243         struct dcerpc_pipe *p = ctx->admin.drsuapi.drs_pipe;
244         struct dcerpc_binding_handle *b = ctx->admin.drsuapi.drs_handle;
245         struct drsuapi_DsReplicaGetInfo r;
246         union drsuapi_DsReplicaGetInfoRequest req;
247         union drsuapi_DsReplicaInfo info;
248         enum drsuapi_DsReplicaInfoType info_type;
249         int i;
250         int invalid_levels = 0;
251         struct {
252                 int32_t level;
253                 int32_t infotype;
254                 const char *obj_dn;
255                 const char *attribute_name;
256                 uint32_t flags;
257         } array[] = {
258                 {
259                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
260                         .infotype = DRSUAPI_DS_REPLICA_INFO_NEIGHBORS
261                 },{
262                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
263                         .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS
264                 },{
265                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
266                         .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA
267                 },{
268                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
269                         .infotype = DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES
270                 },{
271                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
272                         .infotype = DRSUAPI_DS_REPLICA_INFO_KCC_DSA_LINK_FAILURES
273                 },{
274                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
275                         .infotype = DRSUAPI_DS_REPLICA_INFO_PENDING_OPS
276                 },{
277                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
278                         .infotype = DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA
279                 },{
280                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
281                         .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS2
282                 },{
283                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
284                         .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS3
285                 },{
286                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
287                         .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA2,
288                         .obj_dn = "CN=Domain Admins,CN=Users,",
289                         .flags = 0
290                 },{
291                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
292                         .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA2,
293                         .obj_dn = "CN=Domain Admins,CN=Users,",
294                         .flags = DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
295                 },{
296                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
297                         .infotype = DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA2
298                 },{
299                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
300                         .infotype = DRSUAPI_DS_REPLICA_INFO_REPSTO
301                 },{
302                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
303                         .infotype = DRSUAPI_DS_REPLICA_INFO_CLIENT_CONTEXTS
304                 },{
305                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
306                         .infotype = DRSUAPI_DS_REPLICA_INFO_UPTODATE_VECTOR_V1
307                 },{
308                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
309                         .infotype = DRSUAPI_DS_REPLICA_INFO_SERVER_OUTGOING_CALLS
310                 }
311         };
312
313         ctx->domain_dn = torture_get_ldap_base_dn(tctx, p);
314         torture_assert(tctx, ctx->domain_dn != NULL, "Cannot get domain_dn");
315
316         if (torture_setting_bool(tctx, "samba4", false)) {
317                 torture_comment(tctx, "skipping DsReplicaGetInfo test against Samba4\n");
318                 return true;
319         }
320
321         r.in.bind_handle        = &ctx->admin.drsuapi.bind_handle;
322         r.in.req                = &req;
323
324         for (i=0; i < ARRAY_SIZE(array); i++) {
325                 const char *object_dn;
326
327                 torture_comment(tctx, "Testing DsReplicaGetInfo level %d infotype %d\n",
328                                 array[i].level, array[i].infotype);
329
330                 if (array[i].obj_dn) {
331                         object_dn = array[i].obj_dn;
332                         if (object_dn[strlen(object_dn)-1] == ',') {
333                                 /* add the domain DN on the end */
334                                 object_dn = talloc_asprintf(tctx, "%s%s", object_dn, ctx->domain_dn);
335                         }
336                 } else {
337                         object_dn = ctx->domain_dn;
338                 }
339
340                 r.in.level = array[i].level;
341                 switch(r.in.level) {
342                 case DRSUAPI_DS_REPLICA_GET_INFO:
343                         r.in.req->req1.info_type        = array[i].infotype;
344                         r.in.req->req1.object_dn        = object_dn;
345                         ZERO_STRUCT(r.in.req->req1.source_dsa_guid);
346                         break;
347                 case DRSUAPI_DS_REPLICA_GET_INFO2:
348                         r.in.req->req2.info_type        = array[i].infotype;
349                         r.in.req->req2.object_dn        = object_dn;
350                         ZERO_STRUCT(r.in.req->req2.source_dsa_guid);
351                         r.in.req->req2.flags            = 0;
352                         r.in.req->req2.attribute_name   = NULL;
353                         r.in.req->req2.value_dn_str     = NULL;
354                         r.in.req->req2.enumeration_context = 0;
355                         break;
356                 }
357
358                 /* Construct a different request for some of the infoTypes */
359                 if (array[i].attribute_name != NULL) {
360                         r.in.req->req2.attribute_name = array[i].attribute_name;
361                 }
362                 if (array[i].flags != 0) {
363                         r.in.req->req2.flags |= array[i].flags;
364                 }
365
366                 r.out.info              = &info;
367                 r.out.info_type         = &info_type;
368
369                 status = dcerpc_drsuapi_DsReplicaGetInfo_r(b, tctx, &r);
370                 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
371                         torture_comment(tctx,
372                                         "DsReplicaGetInfo level %d and/or infotype %d not supported by server\n",
373                                         array[i].level, array[i].infotype);
374                         continue;
375                 }
376                 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx,
377                                         "DsReplicaGetInfo level %d and/or infotype %d failed\n",
378                                         array[i].level, array[i].infotype));
379                 if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_LEVEL)) {
380                         /* this is a not yet supported level */
381                         torture_comment(tctx,
382                                         "DsReplicaGetInfo level %d and/or infotype %d not yet supported by server\n",
383                                         array[i].level, array[i].infotype);
384                         invalid_levels++;
385                         continue;
386                 }
387
388                 torture_drsuapi_assert_call(tctx, p, status, &r, "dcerpc_drsuapi_DsReplicaGetInfo");
389         }
390
391         if (invalid_levels > 0) {
392                 return false;
393         }
394
395         return true;
396 }
397
398 /**
399  * DSGETINFO test case setup
400  */
401 static bool torture_dsgetinfo_tcase_setup(struct torture_context *tctx, void **data)
402 {
403         bool bret;
404         struct DsGetinfoTest *ctx;
405
406         *data = ctx = test_create_context(tctx);
407         torture_assert(tctx, ctx, "test_create_context() failed");
408
409         bret = _test_DsBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
410         torture_assert(tctx, bret, "_test_DsBind() failed");
411
412         return true;
413 }
414
415 /**
416  * DSGETINFO test case cleanup
417  */
418 static bool torture_dsgetinfo_tcase_teardown(struct torture_context *tctx, void *data)
419 {
420         struct DsGetinfoTest *ctx;
421         struct drsuapi_DsUnbind r;
422         struct policy_handle bind_handle;
423
424         ctx = talloc_get_type(data, struct DsGetinfoTest);
425
426         ZERO_STRUCT(r);
427         r.out.bind_handle = &bind_handle;
428
429         /* Unbing admin handle */
430         r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
431         if (ctx->admin.drsuapi.drs_handle) {
432                 dcerpc_drsuapi_DsUnbind_r(ctx->admin.drsuapi.drs_handle,
433                                           ctx, &r);
434         }
435
436         talloc_free(ctx);
437
438         return true;
439 }
440
441 /**
442  * DSGETINFO test case implementation
443  */
444 void torture_drs_rpc_dsgetinfo_tcase(struct torture_suite *suite)
445 {
446         typedef bool (*run_func) (struct torture_context *test, void *tcase_data);
447         struct torture_tcase *tcase = torture_suite_add_tcase(suite, "dsgetinfo");
448
449         torture_tcase_set_fixture(tcase,
450                                   torture_dsgetinfo_tcase_setup,
451                                   torture_dsgetinfo_tcase_teardown);
452
453         torture_tcase_add_simple_test(tcase, "DsGetReplicaInfo", (run_func)test_getinfo);
454 }
455