lib/util use modules_path(), data_path() and shlib_ext() from source3
[sfrench/samba-autobuild/.git] / source4 / torture / drs / rpc / msds_intid.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    msDS-IntId attribute replication test.
5
6    Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010
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 #include "lib/tsocket/tsocket.h"
35 #include "libcli/resolve/resolve.h"
36
37 struct DsSyncBindInfo {
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 DsaBindInfo {
49         struct dcerpc_binding           *server_binding;
50
51         struct dcerpc_pipe              *drs_pipe;
52         struct dcerpc_binding_handle    *drs_handle;
53
54         DATA_BLOB                       gensec_skey;
55         struct drsuapi_DsBindInfo48     srv_info48;
56         struct policy_handle            rpc_handle;
57 };
58
59 struct DsIntIdTestCtx {
60         const char *ldap_url;
61         const char *domain_dn;
62         const char *config_dn;
63         const char *schema_dn;
64
65         /* what we need to do as 'Administrator' */
66         struct cli_credentials  *creds;
67         struct DsaBindInfo      dsa_bind;
68         struct ldb_context      *ldb;
69
70 };
71
72 /* Format string to create provision LDIF with */
73 #define PROVISION_LDIF_FMT \
74                 "###########################################################\n" \
75                 "# Format string with positional params:\n" \
76                 "#  1 - (int) Unique ID between 1 and 2^16\n" \
77                 "#  2 - (string) Domain DN\n" \
78                 "###########################################################\n" \
79                 "\n" \
80                 "###########################################################\n" \
81                 "# Update schema\n" \
82                 "###########################################################\n" \
83                 "dn: CN=msds-intid-%1$d,CN=Schema,CN=Configuration,%2$s\n" \
84                 "changetype: add\n" \
85                 "objectClass: top\n" \
86                 "objectClass: attributeSchema\n" \
87                 "cn: msds-intid-%1$d\n" \
88                 "attributeID: 1.2.840.%1$d.1.5.9940\n" \
89                 "attributeSyntax: 2.5.5.10\n" \
90                 "omSyntax: 4\n" \
91                 "instanceType: 4\n" \
92                 "isSingleValued: TRUE\n" \
93                 "systemOnly: FALSE\n" \
94                 "\n" \
95                 "# schemaUpdateNow\n" \
96                 "DN:\n" \
97                 "changeType: modify\n" \
98                 "add: schemaUpdateNow\n" \
99                 "schemaUpdateNow: 1\n" \
100                 "-\n" \
101                 "\n" \
102                 "###########################################################\n" \
103                 "# Update User class\n" \
104                 "###########################################################\n" \
105                 "dn: CN=User,CN=Schema,CN=Configuration,%2$s\n" \
106                 "changetype: modify\n" \
107                 "add: mayContain\n" \
108                 "mayContain: msdsIntid%1$d\n" \
109                 "-\n" \
110                 "\n" \
111                 "# schemaUpdateNow\n" \
112                 "DN:\n" \
113                 "changeType: modify\n" \
114                 "add: schemaUpdateNow\n" \
115                 "schemaUpdateNow: 1\n" \
116                 "-\n" \
117                 "\n" \
118                 "###########################################################\n" \
119                 "# create user to test with\n" \
120                 "###########################################################\n" \
121                 "dn: CN=dsIntId_usr_%1$d,CN=Users,%2$s\n" \
122                 "changetype: add\n" \
123                 "objectClass: user\n" \
124                 "cn: dsIntId_usr_%1$d\n" \
125                 "name: dsIntId_usr_%1$d\n" \
126                 "displayName: dsIntId_usr_%1$d\n" \
127                 "sAMAccountName: dsIntId_usr_%1$d\n" \
128                 "msdsIntid%1$d: msDS-IntId-%1$d attribute value\n" \
129                 "\n"
130
131
132 static struct DsIntIdTestCtx *_dsintid_create_context(struct torture_context *tctx)
133 {
134         NTSTATUS status;
135         struct DsIntIdTestCtx *ctx;
136         struct dcerpc_binding *server_binding;
137         const char *binding = torture_setting_string(tctx, "binding", NULL);
138
139         /* Create test suite context */
140         ctx = talloc_zero(tctx, struct DsIntIdTestCtx);
141         if (!ctx) {
142                 torture_result(tctx, TORTURE_FAIL, "Not enough memory!");
143                 return NULL;
144         }
145
146         /* parse binding object */
147         status = dcerpc_parse_binding(ctx, binding, &server_binding);
148         if (!NT_STATUS_IS_OK(status)) {
149                 torture_result(tctx, TORTURE_FAIL,
150                                "Bad binding string '%s': %s", binding, nt_errstr(status));
151                 return NULL;
152         }
153
154         server_binding->flags |= DCERPC_SIGN | DCERPC_SEAL;
155
156         /* populate test suite context */
157         ctx->creds = cmdline_credentials;
158         ctx->dsa_bind.server_binding = server_binding;
159
160         ctx->ldap_url = talloc_asprintf(ctx, "ldap://%s", server_binding->host);
161
162         return ctx;
163 }
164
165 static bool _test_DsaBind(struct torture_context *tctx,
166                          TALLOC_CTX *mem_ctx,
167                          struct cli_credentials *credentials,
168                          uint32_t req_extensions,
169                          struct DsaBindInfo *bi)
170 {
171         NTSTATUS status;
172         struct GUID bind_guid;
173         struct drsuapi_DsBind r;
174         struct drsuapi_DsBindInfoCtr bind_info_ctr;
175         uint32_t supported_extensions;
176
177         /* make DCE RPC connection */
178         status = dcerpc_pipe_connect_b(mem_ctx,
179                                        &bi->drs_pipe,
180                                        bi->server_binding,
181                                        &ndr_table_drsuapi,
182                                        credentials, tctx->ev, tctx->lp_ctx);
183         torture_assert_ntstatus_ok(tctx, status, "Failed to connect to server");
184
185         bi->drs_handle = bi->drs_pipe->binding_handle;
186
187         status = gensec_session_key(bi->drs_pipe->conn->security_state.generic_state,
188                                     &bi->gensec_skey);
189         torture_assert_ntstatus_ok(tctx, status, "failed to get gensec session key");
190
191         /* Bind to DRSUAPI interface */
192         GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &bind_guid);
193
194         /*
195          * Add flags that should be 1, according to MS docs.
196          * It turns out DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3
197          * is actually required in order for GetNCChanges() to
198          * return schemaInfo entry in the prefixMap returned.
199          * Use DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION so
200          * we are able to fetch sensitive data.
201          */
202         supported_extensions = req_extensions
203                              | DRSUAPI_SUPPORTED_EXTENSION_BASE
204                              | DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION
205                              | DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD
206                              | DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3
207                              | DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
208
209         ZERO_STRUCT(bind_info_ctr);
210         bind_info_ctr.length = 28;
211         bind_info_ctr.info.info28.supported_extensions = supported_extensions;
212
213         r.in.bind_guid = &bind_guid;
214         r.in.bind_info = &bind_info_ctr;
215         r.out.bind_handle = &bi->rpc_handle;
216
217         status = dcerpc_drsuapi_DsBind_r(bi->drs_handle, mem_ctx, &r);
218         torture_drsuapi_assert_call(tctx, bi->drs_pipe, status,
219                                     &r, "dcerpc_drsuapi_DsBind_r");
220
221
222         switch (r.out.bind_info->length) {
223         case 24: {
224                 struct drsuapi_DsBindInfo24 *info24;
225                 info24 = &r.out.bind_info->info.info24;
226                 bi->srv_info48.supported_extensions     = info24->supported_extensions;
227                 bi->srv_info48.site_guid                = info24->site_guid;
228                 bi->srv_info48.pid                      = info24->pid;
229                 break;
230         }
231         case 28: {
232                 struct drsuapi_DsBindInfo28 *info28;
233                 info28 = &r.out.bind_info->info.info28;
234                 bi->srv_info48.supported_extensions     = info28->supported_extensions;
235                 bi->srv_info48.site_guid                = info28->site_guid;
236                 bi->srv_info48.pid                      = info28->pid;
237                 bi->srv_info48.repl_epoch               = info28->repl_epoch;
238                 break;
239         }
240         case 48:
241                 bi->srv_info48 = r.out.bind_info->info.info48;
242                 break;
243         default:
244                 torture_result(tctx, TORTURE_FAIL,
245                                "DsBind: unknown BindInfo length: %u",
246                                r.out.bind_info->length);
247                 return false;
248         }
249
250         /* check if server supports extensions we've requested */
251         if ((bi->srv_info48.supported_extensions & req_extensions) != req_extensions) {
252                 torture_result(tctx, TORTURE_FAIL,
253                                "Server does not support requested extensions. "
254                                "Requested: 0x%08X, Supported: 0x%08X",
255                                req_extensions, bi->srv_info48.supported_extensions);
256                 return false;
257         }
258
259         return true;
260 }
261
262 static bool _test_LDAPBind(struct torture_context *tctx,
263                            TALLOC_CTX *mem_ctx,
264                            struct cli_credentials *credentials,
265                            const char *ldap_url,
266                            struct ldb_context **_ldb)
267 {
268         bool ret = true;
269
270         struct ldb_context *ldb;
271
272         const char *modules_option[] = { "modules:paged_searches", NULL };
273         ldb = ldb_init(mem_ctx, tctx->ev);
274         if (ldb == NULL) {
275                 return false;
276         }
277
278         /* Despite us loading the schema from the AD server, we need
279          * the samba handlers to get the extended DN syntax stuff */
280         ret = ldb_register_samba_handlers(ldb);
281         if (ret != LDB_SUCCESS) {
282                 talloc_free(ldb);
283                 return NULL;
284         }
285
286         ldb_set_modules_dir(ldb, modules_path(ldb, "ldb"));
287
288         if (ldb_set_opaque(ldb, "credentials", credentials) != LDB_SUCCESS) {
289                 talloc_free(ldb);
290                 return NULL;
291         }
292
293         if (ldb_set_opaque(ldb, "loadparm", tctx->lp_ctx) != LDB_SUCCESS) {
294                 talloc_free(ldb);
295                 return NULL;
296         }
297
298         ret = ldb_connect(ldb, ldap_url, 0, modules_option);
299         if (ret != LDB_SUCCESS) {
300                 talloc_free(ldb);
301                 torture_assert_int_equal(tctx, ret, LDB_SUCCESS, "Failed to make LDB connection to target");
302         }
303
304         *_ldb = ldb;
305
306         return true;
307 }
308
309 static bool _test_provision(struct torture_context *tctx, struct DsIntIdTestCtx *ctx)
310 {
311         int ret;
312         char *ldif_str;
313         const char *pstr;
314         struct ldb_ldif *ldif;
315         uint32_t attr_id;
316         struct ldb_context *ldb = ctx->ldb;
317
318         /* We must have LDB connection ready by this time */
319         SMB_ASSERT(ldb != NULL);
320
321         ctx->domain_dn = ldb_dn_get_linearized(ldb_get_default_basedn(ldb));
322         torture_assert(tctx, ctx->domain_dn != NULL, "Failed to get Domain DN");
323
324         ctx->config_dn = ldb_dn_get_linearized(ldb_get_config_basedn(ldb));
325         torture_assert(tctx, ctx->config_dn != NULL, "Failed to get Domain DN");
326
327         ctx->schema_dn = ldb_dn_get_linearized(ldb_get_schema_basedn(ldb));
328         torture_assert(tctx, ctx->schema_dn != NULL, "Failed to get Domain DN");
329
330         /* prepare LDIF to provision with */
331         attr_id = generate_random() % 0xFFFF;
332         pstr = ldif_str = talloc_asprintf(ctx, PROVISION_LDIF_FMT,
333                                           attr_id, ctx->domain_dn);
334
335         /* Provision test data */
336         while ((ldif = ldb_ldif_read_string(ldb, &pstr)) != NULL) {
337                 switch (ldif->changetype) {
338                 case LDB_CHANGETYPE_DELETE:
339                         ret = ldb_delete(ldb, ldif->msg->dn);
340                         break;
341                 case LDB_CHANGETYPE_MODIFY:
342                         ret = ldb_modify(ldb, ldif->msg);
343                         break;
344                 case LDB_CHANGETYPE_ADD:
345                 default:
346                         ret = ldb_add(ldb, ldif->msg);
347                         break;
348                 }
349                 if (ret != LDB_SUCCESS) {
350                         char *msg = talloc_asprintf(ctx,
351                                                     "Failed to apply ldif - %s (%s): \n%s",
352                                                     ldb_errstring(ldb),
353                                                     ldb_strerror(ret),
354                                                     ldb_ldif_write_string(ldb, ctx, ldif));
355                         torture_fail(tctx, msg);
356
357                 }
358                 ldb_ldif_read_free(ldb, ldif);
359         }
360
361         return true;
362 }
363
364
365 static bool _test_GetNCChanges(struct torture_context *tctx,
366                                struct DsaBindInfo *bi,
367                                const char *nc_dn_str,
368                                TALLOC_CTX *mem_ctx,
369                                struct drsuapi_DsGetNCChangesCtr6 **_ctr6)
370 {
371         NTSTATUS status;
372         struct drsuapi_DsGetNCChanges r;
373         union drsuapi_DsGetNCChangesRequest req;
374         struct drsuapi_DsReplicaObjectIdentifier nc;
375         struct drsuapi_DsGetNCChangesCtr6 *ctr6_chunk = NULL;
376         struct drsuapi_DsGetNCChangesCtr6 ctr6;
377         uint32_t _level = 0;
378         union drsuapi_DsGetNCChangesCtr ctr;
379
380         struct dom_sid null_sid;
381
382         ZERO_STRUCT(null_sid);
383
384         /* fill-in Naming Context */
385         nc.guid = GUID_zero();
386         nc.sid  = null_sid;
387         nc.dn   = nc_dn_str;
388
389         /* fill-in request fields */
390         req.req8.destination_dsa_guid           = GUID_random();
391         req.req8.source_dsa_invocation_id       = GUID_zero();
392         req.req8.naming_context                 = &nc;
393         req.req8.highwatermark.tmp_highest_usn  = 0;
394         req.req8.highwatermark.reserved_usn     = 0;
395         req.req8.highwatermark.highest_usn      = 0;
396         req.req8.uptodateness_vector            = NULL;
397         req.req8.replica_flags                  = DRSUAPI_DRS_WRIT_REP
398                                                 | DRSUAPI_DRS_INIT_SYNC
399                                                 | DRSUAPI_DRS_PER_SYNC
400                                                 | DRSUAPI_DRS_GET_ANC
401                                                 | DRSUAPI_DRS_NEVER_SYNCED
402                                                 ;
403         req.req8.max_object_count               = 402;
404         req.req8.max_ndr_size                   = 402116;
405
406         req.req8.extended_op                    = DRSUAPI_EXOP_NONE;
407         req.req8.fsmo_info                      = 0;
408         req.req8.partial_attribute_set          = NULL;
409         req.req8.partial_attribute_set_ex       = NULL;
410         req.req8.mapping_ctr.num_mappings       = 0;
411         req.req8.mapping_ctr.mappings           = NULL;
412
413         r.in.bind_handle        = &bi->rpc_handle;
414         r.in.level              = 8;
415         r.in.req                = &req;
416
417         ZERO_STRUCT(r.out);
418         r.out.level_out         = &_level;
419         r.out.ctr               = &ctr;
420
421         ZERO_STRUCT(ctr6);
422         do {
423                 ZERO_STRUCT(ctr);
424
425                 status = dcerpc_drsuapi_DsGetNCChanges_r(bi->drs_handle, mem_ctx, &r);
426                 torture_drsuapi_assert_call(tctx, bi->drs_pipe, status,
427                                             &r, "dcerpc_drsuapi_DsGetNCChanges_r");
428
429                 /* we expect to get level 6 reply */
430                 torture_assert_int_equal(tctx, _level, 6, "Expected level 6 reply");
431
432                 /* store this chunk for later use */
433                 ctr6_chunk = &r.out.ctr->ctr6;
434
435                 if (!ctr6.first_object) {
436                         ctr6 = *ctr6_chunk;
437                 } else {
438                         struct drsuapi_DsReplicaObjectListItemEx *cur;
439
440                         ctr6.object_count += ctr6_chunk->object_count;
441                         for (cur = ctr6.first_object; cur->next_object; cur = cur->next_object) {}
442                         cur->next_object = ctr6_chunk->first_object;
443
444                         /* TODO: store the chunk of linked_attributes if needed */
445                 }
446
447                 /* prepare for next request */
448                 r.in.req->req8.highwatermark = ctr6_chunk->new_highwatermark;
449
450         } while (ctr6_chunk->more_data);
451
452         *_ctr6 = talloc(mem_ctx, struct drsuapi_DsGetNCChangesCtr6);
453         torture_assert(mem_ctx, *_ctr6, "Not enough memory");
454         **_ctr6 = ctr6;
455
456         return true;
457 }
458
459 static char * _make_error_message(TALLOC_CTX *mem_ctx,
460                                   enum drsuapi_DsAttributeId drs_attid,
461                                   const struct dsdb_attribute *dsdb_attr,
462                                   const struct drsuapi_DsReplicaObjectIdentifier *identifier)
463 {
464         return talloc_asprintf(mem_ctx, "\nInvalid ATTID for %1$s (%2$s)\n"
465                                " drs_attid:      %3$11d (0x%3$08X)\n"
466                                " msDS_IntId:     %4$11d (0x%4$08X)\n"
467                                " attributeId_id: %5$11d (0x%5$08X)",
468                                dsdb_attr->lDAPDisplayName,
469                                identifier->dn,
470                                drs_attid,
471                                dsdb_attr->msDS_IntId,
472                                dsdb_attr->attributeID_id);
473 }
474
475 /**
476  * Fetch Schema NC and check ATTID values returned.
477  * When Schema partition is replicated, ATTID
478  * should always be made using prefixMap
479  */
480 static bool test_dsintid_schema(struct torture_context *tctx, struct DsIntIdTestCtx *ctx)
481 {
482         uint32_t i;
483         const struct dsdb_schema *ldap_schema;
484         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
485         const struct dsdb_attribute *dsdb_attr;
486         const struct drsuapi_DsReplicaAttribute *drs_attr;
487         const struct drsuapi_DsReplicaAttributeCtr *attr_ctr;
488         const struct drsuapi_DsReplicaObjectListItemEx *cur;
489         const struct drsuapi_DsReplicaLinkedAttribute *la;
490         TALLOC_CTX *mem_ctx;
491
492         mem_ctx = talloc_new(ctx);
493         torture_assert(tctx, mem_ctx, "Not enough memory");
494
495         /* fetch whole Schema partition */
496         torture_comment(tctx, "Fetch partition: %s\n", ctx->schema_dn);
497         if (!_test_GetNCChanges(tctx, &ctx->dsa_bind, ctx->schema_dn, mem_ctx, &ctr6)) {
498                 torture_fail(tctx, "_test_GetNCChanges() failed");
499         }
500
501         /* load schema if not loaded yet */
502         torture_comment(tctx, "Loading schema...\n");
503         if (!drs_util_dsdb_schema_load_ldb(tctx, ctx->ldb, &ctr6->mapping_ctr, false)) {
504                 torture_fail(tctx, "drs_util_dsdb_schema_load_ldb() failed");
505         }
506         ldap_schema = dsdb_get_schema(ctx->ldb, NULL);
507
508         /* verify ATTIDs fetched */
509         torture_comment(tctx, "Verify ATTIDs fetched\n");
510         for (cur = ctr6->first_object; cur; cur = cur->next_object) {
511                 attr_ctr = &cur->object.attribute_ctr;
512                 for (i = 0; i < attr_ctr->num_attributes; i++) {
513                         drs_attr = &attr_ctr->attributes[i];
514                         dsdb_attr = dsdb_attribute_by_attributeID_id(ldap_schema,
515                                                                      drs_attr->attid);
516
517                         torture_assert(tctx,
518                                        drs_attr->attid == dsdb_attr->attributeID_id,
519                                        _make_error_message(ctx, drs_attr->attid,
520                                                            dsdb_attr,
521                                                            cur->object.identifier));
522                         if (dsdb_attr->msDS_IntId) {
523                                 torture_assert(tctx,
524                                                drs_attr->attid != dsdb_attr->msDS_IntId,
525                                                _make_error_message(ctx, drs_attr->attid,
526                                                                    dsdb_attr,
527                                                                    cur->object.identifier));
528                         }
529                 }
530         }
531
532         /* verify ATTIDs for Linked Attributes */
533         torture_comment(tctx, "Verify ATTIDs for Linked Attributes (%u)\n",
534                         ctr6->linked_attributes_count);
535         for (i = 0; i < ctr6->linked_attributes_count; i++) {
536                 la = &ctr6->linked_attributes[i];
537                 dsdb_attr = dsdb_attribute_by_attributeID_id(ldap_schema, la->attid);
538
539                 torture_assert(tctx,
540                                la->attid == dsdb_attr->attributeID_id,
541                                _make_error_message(ctx, la->attid,
542                                                    dsdb_attr,
543                                                    la->identifier))
544                 if (dsdb_attr->msDS_IntId) {
545                         torture_assert(tctx,
546                                        la->attid != dsdb_attr->msDS_IntId,
547                                        _make_error_message(ctx, la->attid,
548                                                            dsdb_attr,
549                                                            la->identifier))
550                 }
551         }
552
553         talloc_free(mem_ctx);
554
555         return true;
556 }
557
558 /**
559  * Fetch non-Schema NC and check ATTID values returned.
560  * When non-Schema partition is replicated, ATTID
561  * should be msDS-IntId value for the attribute
562  * if this value exists
563  */
564 static bool _test_dsintid(struct torture_context *tctx,
565                           struct DsIntIdTestCtx *ctx,
566                           const char *nc_dn_str)
567 {
568         uint32_t i;
569         const struct dsdb_schema *ldap_schema;
570         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
571         const struct dsdb_attribute *dsdb_attr;
572         const struct drsuapi_DsReplicaAttribute *drs_attr;
573         const struct drsuapi_DsReplicaAttributeCtr *attr_ctr;
574         const struct drsuapi_DsReplicaObjectListItemEx *cur;
575         const struct drsuapi_DsReplicaLinkedAttribute *la;
576         TALLOC_CTX *mem_ctx;
577
578         mem_ctx = talloc_new(ctx);
579         torture_assert(tctx, mem_ctx, "Not enough memory");
580
581         /* fetch whole Schema partition */
582         torture_comment(tctx, "Fetch partition: %s\n", nc_dn_str);
583         if (!_test_GetNCChanges(tctx, &ctx->dsa_bind, nc_dn_str, mem_ctx, &ctr6)) {
584                 torture_fail(tctx, "_test_GetNCChanges() failed");
585         }
586
587         /* load schema if not loaded yet */
588         torture_comment(tctx, "Loading schema...\n");
589         if (!drs_util_dsdb_schema_load_ldb(tctx, ctx->ldb, &ctr6->mapping_ctr, false)) {
590                 torture_fail(tctx, "drs_util_dsdb_schema_load_ldb() failed");
591         }
592         ldap_schema = dsdb_get_schema(ctx->ldb, NULL);
593
594         /* verify ATTIDs fetched */
595         torture_comment(tctx, "Verify ATTIDs fetched\n");
596         for (cur = ctr6->first_object; cur; cur = cur->next_object) {
597                 attr_ctr = &cur->object.attribute_ctr;
598                 for (i = 0; i < attr_ctr->num_attributes; i++) {
599                         drs_attr = &attr_ctr->attributes[i];
600                         dsdb_attr = dsdb_attribute_by_attributeID_id(ldap_schema,
601                                                                      drs_attr->attid);
602                         if (dsdb_attr->msDS_IntId) {
603                                 torture_assert(tctx,
604                                                drs_attr->attid == dsdb_attr->msDS_IntId,
605                                                _make_error_message(ctx, drs_attr->attid,
606                                                                    dsdb_attr,
607                                                                    cur->object.identifier));
608                         } else {
609                                 torture_assert(tctx,
610                                                drs_attr->attid == dsdb_attr->attributeID_id,
611                                                _make_error_message(ctx, drs_attr->attid,
612                                                                    dsdb_attr,
613                                                                    cur->object.identifier));
614                         }
615                 }
616         }
617
618         /* verify ATTIDs for Linked Attributes */
619         torture_comment(tctx, "Verify ATTIDs for Linked Attributes (%u)\n",
620                         ctr6->linked_attributes_count);
621         for (i = 0; i < ctr6->linked_attributes_count; i++) {
622                 la = &ctr6->linked_attributes[i];
623                 dsdb_attr = dsdb_attribute_by_attributeID_id(ldap_schema, la->attid);
624
625                 if (dsdb_attr->msDS_IntId) {
626                         torture_assert(tctx,
627                                        la->attid == dsdb_attr->msDS_IntId,
628                                        _make_error_message(ctx, la->attid,
629                                                            dsdb_attr,
630                                                            la->identifier));
631                 } else {
632                         torture_assert(tctx,
633                                        la->attid == dsdb_attr->attributeID_id,
634                                        _make_error_message(ctx, la->attid,
635                                                            dsdb_attr,
636                                                            la->identifier));
637                 }
638         }
639
640         talloc_free(mem_ctx);
641
642         return true;
643 }
644
645 /**
646  * Fetch Domain NC and check ATTID values returned.
647  * When Domain partition is replicated, ATTID
648  * should be msDS-IntId value for the attribute
649  * if this value exists
650  */
651 static bool test_dsintid_configuration(struct torture_context *tctx, struct DsIntIdTestCtx *ctx)
652 {
653         return _test_dsintid(tctx, ctx, ctx->config_dn);
654 }
655
656 /**
657  * Fetch Configuration NC and check ATTID values returned.
658  * When Configuration partition is replicated, ATTID
659  * should be msDS-IntId value for the attribute
660  * if this value exists
661  */
662 static bool test_dsintid_domain(struct torture_context *tctx, struct DsIntIdTestCtx *ctx)
663 {
664         return _test_dsintid(tctx, ctx, ctx->domain_dn);
665 }
666
667
668 /**
669  * DSSYNC test case setup
670  */
671 static bool torture_dsintid_tcase_setup(struct torture_context *tctx, void **data)
672 {
673         bool bret;
674         struct DsIntIdTestCtx *ctx;
675
676         *data = ctx = _dsintid_create_context(tctx);
677         torture_assert(tctx, ctx, "test_create_context() failed");
678
679         bret = _test_DsaBind(tctx, ctx, ctx->creds,
680                              DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8 |
681                              DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6,
682                              &ctx->dsa_bind);
683         torture_assert(tctx, bret, "_test_DsaBind() failed");
684
685         bret = _test_LDAPBind(tctx, ctx, ctx->creds, ctx->ldap_url, &ctx->ldb);
686         torture_assert(tctx, bret, "_test_LDAPBind() failed");
687
688         bret = _test_provision(tctx, ctx);
689         torture_assert(tctx, bret, "_test_provision() failed");
690
691         return true;
692 }
693
694 /**
695  * DSSYNC test case cleanup
696  */
697 static bool torture_dsintid_tcase_teardown(struct torture_context *tctx, void *data)
698 {
699         struct DsIntIdTestCtx *ctx;
700         struct drsuapi_DsUnbind r;
701         struct policy_handle bind_handle;
702
703         ctx = talloc_get_type(data, struct DsIntIdTestCtx);
704
705         ZERO_STRUCT(r);
706         r.out.bind_handle = &bind_handle;
707
708         /* Release DRSUAPI handle */
709         r.in.bind_handle = &ctx->dsa_bind.rpc_handle;
710         dcerpc_drsuapi_DsUnbind_r(ctx->dsa_bind.drs_handle, ctx, &r);
711
712         talloc_free(ctx);
713
714         return true;
715 }
716
717 /**
718  * DSSYNC test case implementation
719  */
720 void torture_drs_rpc_dsintid_tcase(struct torture_suite *suite)
721 {
722         typedef bool (*run_func) (struct torture_context *test, void *tcase_data);
723
724         struct torture_test *test;
725         struct torture_tcase *tcase = torture_suite_add_tcase(suite, "msDSIntId");
726
727         torture_tcase_set_fixture(tcase,
728                                   torture_dsintid_tcase_setup,
729                                   torture_dsintid_tcase_teardown);
730
731         test = torture_tcase_add_simple_test(tcase, "Schema", (run_func)test_dsintid_schema);
732         test = torture_tcase_add_simple_test(tcase, "Configuration", (run_func)test_dsintid_configuration);
733         test = torture_tcase_add_simple_test(tcase, "Domain", (run_func)test_dsintid_domain);
734 }