]> git.samba.org - kai/samba.git/blob - source4/torture/rpc/dsgetinfo.c
s4:torture/rpc/dsgetinfo.c: check for NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE instead...
[kai/samba.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/torture_rpc.h"
34 #include "torture/drs/proto.h"
35
36
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;
46 };
47
48 struct DsGetinfoTest {
49         struct dcerpc_binding *drsuapi_binding;
50
51         const char *ldap_url;
52         const char *site_name;
53
54         const char *domain_dn;
55
56         /* what we need to do as 'Administrator' */
57         struct {
58                 struct cli_credentials *credentials;
59                 struct DsGetinfoBindInfo drsuapi;
60         } admin;
61 };
62
63
64
65 /*
66   return the default DN for a ldap server given a connected RPC pipe to the
67   server
68  */
69 static const char *torture_get_ldap_base_dn(struct torture_context *tctx, struct dcerpc_pipe *p)
70 {
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;
79
80         ldb = ldb_init(tmp_ctx, tctx->ev);
81         if (ldb == NULL) {
82                 talloc_free(tmp_ctx);
83                 return NULL;
84         }
85
86         if (ldb_set_opaque(ldb, "loadparm", tctx->lp_ctx)) {
87                 talloc_free(ldb);
88                 return NULL;
89         }
90
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;
96         }
97
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;
104         }
105
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;
110 }
111
112
113 static struct DsGetinfoTest *test_create_context(struct torture_context *tctx)
114 {
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;
122
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;
127         }
128         ctx->drsuapi_binding->flags |= DCERPC_SIGN | DCERPC_SEAL;
129
130         /* ctx->admin ...*/
131         ctx->admin.credentials                          = cmdline_credentials;
132
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;
139
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;
143
144         GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
145
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;
149
150         return ctx;
151 }
152
153 static bool _test_DsBind(struct torture_context *tctx,
154                          struct DsGetinfoTest *ctx, struct cli_credentials *credentials, struct DsGetinfoBindInfo *b)
155 {
156         NTSTATUS status;
157         bool ret = true;
158
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);
163
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;
167         }
168         b->drs_handle = b->drs_pipe->binding_handle;
169
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                 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
174                 ret = false;
175         } else if (!W_ERROR_IS_OK(b->req.out.result)) {
176                 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
177                 ret = false;
178         }
179
180         ZERO_STRUCT(b->peer_bind_info28);
181         if (b->req.out.bind_info) {
182                 switch (b->req.out.bind_info->length) {
183                 case 24: {
184                         struct drsuapi_DsBindInfo24 *info24;
185                         info24 = &b->req.out.bind_info->info.info24;
186                         b->peer_bind_info28.supported_extensions= info24->supported_extensions;
187                         b->peer_bind_info28.site_guid           = info24->site_guid;
188                         b->peer_bind_info28.pid                 = info24->pid;
189                         b->peer_bind_info28.repl_epoch          = 0;
190                         break;
191                 }
192                 case 48: {
193                         struct drsuapi_DsBindInfo48 *info48;
194                         info48 = &b->req.out.bind_info->info.info48;
195                         b->peer_bind_info28.supported_extensions= info48->supported_extensions;
196                         b->peer_bind_info28.site_guid           = info48->site_guid;
197                         b->peer_bind_info28.pid                 = info48->pid;
198                         b->peer_bind_info28.repl_epoch          = info48->repl_epoch;
199                         break;
200                 }
201                 case 28:
202                         b->peer_bind_info28 = b->req.out.bind_info->info.info28;
203                         break;
204                 default:
205                         printf("DsBind - warning: unknown BindInfo length: %u\n",
206                                b->req.out.bind_info->length);
207                 }
208         }
209
210         return ret;
211 }
212
213
214 static bool test_getinfo(struct torture_context *tctx,
215                          struct DsGetinfoTest *ctx)
216 {
217         NTSTATUS status;
218         struct dcerpc_pipe *p = ctx->admin.drsuapi.drs_pipe;
219         struct dcerpc_binding_handle *b = ctx->admin.drsuapi.drs_handle;
220         struct drsuapi_DsReplicaGetInfo r;
221         union drsuapi_DsReplicaGetInfoRequest req;
222         union drsuapi_DsReplicaInfo info;
223         enum drsuapi_DsReplicaInfoType info_type;
224         int i;
225         int invalid_levels = 0;
226         struct {
227                 int32_t level;
228                 int32_t infotype;
229                 const char *obj_dn;
230                 const char *attribute_name;
231                 uint32_t flags;
232         } array[] = {
233                 {
234                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
235                         .infotype = DRSUAPI_DS_REPLICA_INFO_NEIGHBORS
236                 },{
237                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
238                         .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS
239                 },{
240                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
241                         .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA
242                 },{
243                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
244                         .infotype = DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES
245                 },{
246                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
247                         .infotype = DRSUAPI_DS_REPLICA_INFO_KCC_DSA_LINK_FAILURES
248                 },{
249                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
250                         .infotype = DRSUAPI_DS_REPLICA_INFO_PENDING_OPS
251                 },{
252                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
253                         .infotype = DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA
254                 },{
255                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
256                         .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS2
257                 },{
258                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
259                         .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS3
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 = 0
265                 },{
266                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
267                         .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA2,
268                         .obj_dn = "CN=Domain Admins,CN=Users,",
269                         .flags = DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
270                 },{
271                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
272                         .infotype = DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA2
273                 },{
274                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
275                         .infotype = DRSUAPI_DS_REPLICA_INFO_REPSTO
276                 },{
277                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
278                         .infotype = DRSUAPI_DS_REPLICA_INFO_CLIENT_CONTEXTS
279                 },{
280                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
281                         .infotype = DRSUAPI_DS_REPLICA_INFO_UPTODATE_VECTOR_V1
282                 },{
283                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
284                         .infotype = DRSUAPI_DS_REPLICA_INFO_SERVER_OUTGOING_CALLS
285                 }
286         };
287
288         ctx->domain_dn = torture_get_ldap_base_dn(tctx, p);
289         torture_assert(tctx, ctx->domain_dn != NULL, "Cannot get domain_dn");
290
291         if (torture_setting_bool(tctx, "samba4", false)) {
292                 torture_comment(tctx, "skipping DsReplicaGetInfo test against Samba4\n");
293                 return true;
294         }
295
296         r.in.bind_handle        = &ctx->admin.drsuapi.bind_handle;
297         r.in.req                = &req;
298
299         for (i=0; i < ARRAY_SIZE(array); i++) {
300                 const char *object_dn;
301
302                 torture_comment(tctx, "Testing DsReplicaGetInfo level %d infotype %d\n",
303                                 array[i].level, array[i].infotype);
304
305                 if (array[i].obj_dn) {
306                         object_dn = array[i].obj_dn;
307                         if (object_dn[strlen(object_dn)-1] == ',') {
308                                 /* add the domain DN on the end */
309                                 object_dn = talloc_asprintf(tctx, "%s%s", object_dn, ctx->domain_dn);
310                         }
311                 } else {
312                         object_dn = ctx->domain_dn;
313                 }
314
315                 r.in.level = array[i].level;
316                 switch(r.in.level) {
317                 case DRSUAPI_DS_REPLICA_GET_INFO:
318                         r.in.req->req1.info_type        = array[i].infotype;
319                         r.in.req->req1.object_dn        = object_dn;
320                         ZERO_STRUCT(r.in.req->req1.source_dsa_guid);
321                         break;
322                 case DRSUAPI_DS_REPLICA_GET_INFO2:
323                         r.in.req->req2.info_type        = array[i].infotype;
324                         r.in.req->req2.object_dn        = object_dn;
325                         ZERO_STRUCT(r.in.req->req2.source_dsa_guid);
326                         r.in.req->req2.flags            = 0;
327                         r.in.req->req2.attribute_name   = NULL;
328                         r.in.req->req2.value_dn_str     = NULL;
329                         r.in.req->req2.enumeration_context = 0;
330                         break;
331                 }
332
333                 /* Construct a different request for some of the infoTypes */
334                 if (array[i].attribute_name != NULL) {
335                         r.in.req->req2.attribute_name = array[i].attribute_name;
336                 }
337                 if (array[i].flags != 0) {
338                         r.in.req->req2.flags |= array[i].flags;
339                 }
340
341                 r.out.info              = &info;
342                 r.out.info_type         = &info_type;
343
344                 status = dcerpc_drsuapi_DsReplicaGetInfo_r(b, tctx, &r);
345                 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
346                         torture_comment(tctx,
347                                         "DsReplicaGetInfo level %d and/or infotype %d not supported by server\n",
348                                         array[i].level, array[i].infotype);
349                         continue;
350                 }
351                 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx,
352                                         "DsReplicaGetInfo level %d and/or infotype %d failed\n",
353                                         array[i].level, array[i].infotype));
354                 if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_LEVEL)) {
355                         /* this is a not yet supported level */
356                         torture_comment(tctx,
357                                         "DsReplicaGetInfo level %d and/or infotype %d not yet supported by server\n",
358                                         array[i].level, array[i].infotype);
359                         invalid_levels++;
360                         continue;
361                 }
362
363                 torture_drsuapi_assert_call(tctx, p, status, &r, "dcerpc_drsuapi_DsReplicaGetInfo");
364         }
365
366         if (invalid_levels > 0) {
367                 return false;
368         }
369
370         return true;
371 }
372
373 /**
374  * DSGETINFO test case setup
375  */
376 static bool torture_dsgetinfo_tcase_setup(struct torture_context *tctx, void **data)
377 {
378         bool bret;
379         struct DsGetinfoTest *ctx;
380
381         *data = ctx = test_create_context(tctx);
382         torture_assert(tctx, ctx, "test_create_context() failed");
383
384         bret = _test_DsBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
385         torture_assert(tctx, bret, "_test_DsBind() failed");
386
387         return true;
388 }
389
390 /**
391  * DSGETINFO test case cleanup
392  */
393 static bool torture_dsgetinfo_tcase_teardown(struct torture_context *tctx, void *data)
394 {
395         struct DsGetinfoTest *ctx;
396         struct drsuapi_DsUnbind r;
397         struct policy_handle bind_handle;
398
399         ctx = talloc_get_type(data, struct DsGetinfoTest);
400
401         ZERO_STRUCT(r);
402         r.out.bind_handle = &bind_handle;
403
404         /* Unbing admin handle */
405         r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
406         dcerpc_drsuapi_DsUnbind_r(ctx->admin.drsuapi.drs_handle, ctx, &r);
407
408         talloc_free(ctx);
409
410         return true;
411 }
412
413 /**
414  * DSGETINFO test case implementation
415  */
416 void torture_drs_rpc_dsgetinfo_tcase(struct torture_suite *suite)
417 {
418         typedef bool (*run_func) (struct torture_context *test, void *tcase_data);
419
420         struct torture_test *test;
421         struct torture_tcase *tcase = torture_suite_add_tcase(suite, "DSGETINFO");
422
423         torture_tcase_set_fixture(tcase,
424                                   torture_dsgetinfo_tcase_setup,
425                                   torture_dsgetinfo_tcase_teardown);
426
427         test = torture_tcase_add_simple_test(tcase, "DsGetReplicaInfo", (run_func)test_getinfo);
428 }
429