Merge branch 'master' of ssh://git.samba.org/data/git/samba
[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/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                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
174                         errstr = dcerpc_errstr(ctx, b->drs_pipe->last_fault_code);
175                 }
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;
181         }
182
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;
194                 }
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;
203                 }
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);
210                 }
211         }
212
213         return ret;
214 }
215
216
217 static bool test_getinfo(struct torture_context *tctx,
218                          struct DsGetinfoTest *ctx)
219 {
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[] = {
236                 {
237                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
238                         .infotype = DRSUAPI_DS_REPLICA_INFO_NEIGHBORS
239                 },{
240                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
241                         .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS
242                 },{
243                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
244                         .infotype = DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA
245                 },{
246                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
247                         .infotype = DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES
248                 },{
249                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
250                         .infotype = DRSUAPI_DS_REPLICA_INFO_KCC_DSA_LINK_FAILURES
251                 },{
252                         .level = DRSUAPI_DS_REPLICA_GET_INFO,
253                         .infotype = DRSUAPI_DS_REPLICA_INFO_PENDING_OPS
254                 },{
255                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
256                         .infotype = DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA
257                 },{
258                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
259                         .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS2
260                 },{
261                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
262                         .infotype = DRSUAPI_DS_REPLICA_INFO_CURSORS3
263                 },{
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
268                 },{
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
273                 },{
274                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
275                         .infotype = DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA2
276                 },{
277                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
278                         .infotype = DRSUAPI_DS_REPLICA_INFO_REPSTO
279                 },{
280                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
281                         .infotype = DRSUAPI_DS_REPLICA_INFO_CLIENT_CONTEXTS
282                 },{
283                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
284                         .infotype = DRSUAPI_DS_REPLICA_INFO_UPTODATE_VECTOR_V1
285                 },{
286                         .level = DRSUAPI_DS_REPLICA_GET_INFO2,
287                         .infotype = DRSUAPI_DS_REPLICA_INFO_SERVER_OUTGOING_CALLS
288                 }
289         };
290
291         ctx->domain_dn = torture_get_ldap_base_dn(tctx, p);
292         torture_assert(tctx, ctx->domain_dn != NULL, "Cannot get domain_dn");
293
294         if (torture_setting_bool(tctx, "samba4", false)) {
295                 torture_comment(tctx, "skipping DsReplicaGetInfo test against Samba4\n");
296                 return true;
297         }
298
299         r.in.bind_handle        = &ctx->admin.drsuapi.bind_handle;
300         r.in.req                = &req;
301
302         for (i=0; i < ARRAY_SIZE(array); i++) {
303                 const char *object_dn;
304
305                 torture_comment(tctx, "Testing DsReplicaGetInfo level %d infotype %d\n",
306                                 array[i].level, array[i].infotype);
307
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);
313                         }
314                 } else {
315                         object_dn = ctx->domain_dn;
316                 }
317
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;
334                 }
335
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;
339                 }
340                 if (array[i].flags != 0) {
341                         r.in.req->req2.flags |= array[i].flags;
342                 }
343
344                 r.out.info              = &info;
345                 r.out.info_type         = &info_type;
346
347                 status = dcerpc_drsuapi_DsReplicaGetInfo_r(b, tctx, &r);
348                 if (!NT_STATUS_IS_OK(status) && p->last_fault_code == DCERPC_FAULT_INVALID_TAG) {
349                         torture_comment(tctx,
350                                         "DsReplicaGetInfo level %d and/or infotype %d not supported by server\n",
351                                         array[i].level, array[i].infotype);
352                         continue;
353                 }
354                 torture_assert_ntstatus_ok(tctx, status, talloc_asprintf(tctx,
355                                         "DsReplicaGetInfo level %d and/or infotype %d failed\n",
356                                         array[i].level, array[i].infotype));
357                 if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_LEVEL)) {
358                         /* this is a not yet supported level */
359                         torture_comment(tctx,
360                                         "DsReplicaGetInfo level %d and/or infotype %d not yet supported by server\n",
361                                         array[i].level, array[i].infotype);
362                         invalid_levels++;
363                         continue;
364                 }
365
366                 torture_drsuapi_assert_call(tctx, p, status, &r, "dcerpc_drsuapi_DsReplicaGetInfo");
367         }
368
369         if (invalid_levels > 0) {
370                 return false;
371         }
372
373         return true;
374 }
375
376 /**
377  * DSGETINFO test case setup
378  */
379 static bool torture_dsgetinfo_tcase_setup(struct torture_context *tctx, void **data)
380 {
381         bool bret;
382         struct DsGetinfoTest *ctx;
383
384         *data = ctx = test_create_context(tctx);
385         torture_assert(tctx, ctx, "test_create_context() failed");
386
387         bret = _test_DsBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
388         torture_assert(tctx, bret, "_test_DsBind() failed");
389
390         return true;
391 }
392
393 /**
394  * DSGETINFO test case cleanup
395  */
396 static bool torture_dsgetinfo_tcase_teardown(struct torture_context *tctx, void *data)
397 {
398         struct DsGetinfoTest *ctx;
399         struct drsuapi_DsUnbind r;
400         struct policy_handle bind_handle;
401
402         ctx = talloc_get_type(data, struct DsGetinfoTest);
403
404         ZERO_STRUCT(r);
405         r.out.bind_handle = &bind_handle;
406
407         /* Unbing admin handle */
408         r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
409         dcerpc_drsuapi_DsUnbind_r(ctx->admin.drsuapi.drs_handle, ctx, &r);
410
411         talloc_free(ctx);
412
413         return true;
414 }
415
416 /**
417  * DSGETINFO test case implementation
418  */
419 void torture_drs_rpc_dsgetinfo_tcase(struct torture_suite *suite)
420 {
421         typedef bool (*run_func) (struct torture_context *test, void *tcase_data);
422
423         struct torture_test *test;
424         struct torture_tcase *tcase = torture_suite_add_tcase(suite, "DSGETINFO");
425
426         torture_tcase_set_fixture(tcase,
427                                   torture_dsgetinfo_tcase_setup,
428                                   torture_dsgetinfo_tcase_teardown);
429
430         test = torture_tcase_add_simple_test(tcase, "DsGetReplicaInfo", (run_func)test_getinfo);
431 }
432