r25554: Convert last instances of BOOL, True and False to the standard types.
[kai/samba-autobuild/.git] / source4 / torture / rpc / drsuapi.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    DRSUapi tests
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Stefan (metze) Metzmacher 2004
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2006
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "torture/torture.h"
26 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
27 #include "torture/rpc/rpc.h"
28 #include "param/param.h"
29
30 #define TEST_MACHINE_NAME "torturetest"
31
32 bool test_DsBind(struct dcerpc_pipe *p, struct torture_context *tctx,
33                  struct DsPrivate *priv)
34 {
35         NTSTATUS status;
36         struct drsuapi_DsBind r;
37
38         GUID_from_string(DRSUAPI_DS_BIND_GUID, &priv->bind_guid);
39
40         r.in.bind_guid = &priv->bind_guid;
41         r.in.bind_info = NULL;
42         r.out.bind_handle = &priv->bind_handle;
43
44         torture_comment(tctx, "testing DsBind\n");
45
46         status = dcerpc_drsuapi_DsBind(p, tctx, &r);
47         if (!NT_STATUS_IS_OK(status)) {
48                 const char *errstr = nt_errstr(status);
49                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
50                         errstr = dcerpc_errstr(tctx, p->last_fault_code);
51                 }
52                 torture_fail(tctx, "dcerpc_drsuapi_DsBind failed");
53         } else if (!W_ERROR_IS_OK(r.out.result)) {
54                 torture_fail(tctx, "DsBind failed");
55         }
56
57         return true;
58 }
59
60 static bool test_DsGetDomainControllerInfo(struct dcerpc_pipe *p, struct torture_context *torture, 
61                       struct DsPrivate *priv)
62 {
63         NTSTATUS status;
64         struct drsuapi_DsGetDomainControllerInfo r;
65         bool found = false;
66         int i, j, k;
67         
68         struct {
69                 const char *name;
70                 WERROR expected;
71         } names[] = { 
72                 {       
73                         .name = torture_join_dom_netbios_name(priv->join),
74                         .expected = WERR_OK
75                 },
76                 {
77                         .name = torture_join_dom_dns_name(priv->join),
78                         .expected = WERR_OK
79                 },
80                 {
81                         .name = "__UNKNOWN_DOMAIN__",
82                         .expected = WERR_DS_OBJ_NOT_FOUND
83                 },
84                 {
85                         .name = "unknown.domain.samba.example.com",
86                         .expected = WERR_DS_OBJ_NOT_FOUND
87                 },
88         };
89         int levels[] = {1, 2};
90         int level;
91
92         for (i=0; i < ARRAY_SIZE(levels); i++) {
93                 for (j=0; j < ARRAY_SIZE(names); j++) {
94                         level = levels[i];
95                         r.in.bind_handle = &priv->bind_handle;
96                         r.in.level = 1;
97                         
98                         r.in.req.req1.domain_name = names[j].name;
99                         r.in.req.req1.level = level;
100                         
101                         torture_comment(torture,
102                                    "testing DsGetDomainControllerInfo level %d on domainname '%s'\n",
103                                r.in.req.req1.level, r.in.req.req1.domain_name);
104                 
105                         status = dcerpc_drsuapi_DsGetDomainControllerInfo(p, torture, &r);
106                         torture_assert_ntstatus_ok(torture, status,
107                                    "dcerpc_drsuapi_DsGetDomainControllerInfo with dns domain failed");
108                         torture_assert_werr_equal(torture, 
109                                                                           r.out.result, names[j].expected, 
110                                            "DsGetDomainControllerInfo level with dns domain failed");
111                 
112                         if (!W_ERROR_IS_OK(r.out.result)) {
113                                 /* If this was an error, we can't read the result structure */
114                                 continue;
115                         }
116
117                         torture_assert_int_equal(torture, 
118                                                                          r.in.req.req1.level, r.out.level_out, 
119                                                                          "dcerpc_drsuapi_DsGetDomainControllerInfo level"); 
120
121                         switch (level) {
122                         case 1:
123                                 for (k=0; k < r.out.ctr.ctr1.count; k++) {
124                                         if (strcasecmp_m(r.out.ctr.ctr1.array[k].netbios_name, 
125                                                          torture_join_netbios_name(priv->join)) == 0) {
126                                                 found = true;
127                                                 break;
128                                         }
129                                 }
130                                 break;
131                         case 2:
132                                 for (k=0; k < r.out.ctr.ctr2.count; k++) {
133                                         if (strcasecmp_m(r.out.ctr.ctr2.array[k].netbios_name, 
134                                                          torture_join_netbios_name(priv->join)) == 0) {
135                                                 found = true;
136                                                 priv->dcinfo    = r.out.ctr.ctr2.array[k];
137                                                 break;
138                                         }
139                                 }
140                                 break;
141                         }
142                         torture_assert(torture, found,
143                                  "dcerpc_drsuapi_DsGetDomainControllerInfo: Failed to find the domain controller we just created during the join");
144                 }
145         }
146
147         r.in.bind_handle = &priv->bind_handle;
148         r.in.level = 1;
149         
150         r.in.req.req1.domain_name = "__UNKNOWN_DOMAIN__"; /* This is clearly ignored for this level */
151         r.in.req.req1.level = -1;
152         
153         printf("testing DsGetDomainControllerInfo level %d on domainname '%s'\n",
154                r.in.req.req1.level, r.in.req.req1.domain_name);
155         
156         status = dcerpc_drsuapi_DsGetDomainControllerInfo(p, torture, &r);
157
158         torture_assert_ntstatus_ok(torture, status, 
159                         "dcerpc_drsuapi_DsGetDomainControllerInfo with dns domain failed");
160         torture_assert_werr_ok(torture, r.out.result, 
161                            "DsGetDomainControllerInfo with dns domain failed");
162         
163         {
164                 const char *dc_account = talloc_asprintf(torture, "%s\\%s$",
165                                                          torture_join_dom_netbios_name(priv->join), 
166                                                          priv->dcinfo.netbios_name);
167                 for (k=0; k < r.out.ctr.ctr01.count; k++) {
168                         if (strcasecmp_m(r.out.ctr.ctr01.array[k].client_account, 
169                                          dc_account)) {
170                                 found = true;
171                                 break;
172                         }
173                 }
174                 torture_assert(torture, found,
175                         "dcerpc_drsuapi_DsGetDomainControllerInfo level: Failed to find the domain controller in last logon records");
176         }
177
178
179         return true;
180 }
181
182 static bool test_DsWriteAccountSpn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
183                                    struct DsPrivate *priv)
184 {
185         NTSTATUS status;
186         struct drsuapi_DsWriteAccountSpn r;
187         struct drsuapi_DsNameString names[2];
188         bool ret = true;
189
190         r.in.bind_handle                = &priv->bind_handle;
191         r.in.level                      = 1;
192
193         printf("testing DsWriteAccountSpn\n");
194
195         r.in.req.req1.operation = DRSUAPI_DS_SPN_OPERATION_ADD;
196         r.in.req.req1.unknown1  = 0;
197         r.in.req.req1.object_dn = priv->dcinfo.computer_dn;
198         r.in.req.req1.count     = 2;
199         r.in.req.req1.spn_names = names;
200         names[0].str = talloc_asprintf(mem_ctx, "smbtortureSPN/%s",priv->dcinfo.netbios_name);
201         names[1].str = talloc_asprintf(mem_ctx, "smbtortureSPN/%s",priv->dcinfo.dns_name);
202
203         status = dcerpc_drsuapi_DsWriteAccountSpn(p, mem_ctx, &r);
204         if (!NT_STATUS_IS_OK(status)) {
205                 const char *errstr = nt_errstr(status);
206                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
207                         errstr = dcerpc_errstr(mem_ctx, p->last_fault_code);
208                 }
209                 printf("dcerpc_drsuapi_DsWriteAccountSpn failed - %s\n", errstr);
210                 ret = false;
211         } else if (!W_ERROR_IS_OK(r.out.result)) {
212                 printf("DsWriteAccountSpn failed - %s\n", win_errstr(r.out.result));
213                 ret = false;
214         }
215
216         r.in.req.req1.operation = DRSUAPI_DS_SPN_OPERATION_DELETE;
217         r.in.req.req1.unknown1  = 0;
218
219         status = dcerpc_drsuapi_DsWriteAccountSpn(p, mem_ctx, &r);
220         if (!NT_STATUS_IS_OK(status)) {
221                 const char *errstr = nt_errstr(status);
222                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
223                         errstr = dcerpc_errstr(mem_ctx, p->last_fault_code);
224                 }
225                 printf("dcerpc_drsuapi_DsWriteAccountSpn failed - %s\n", errstr);
226                 ret = false;
227         } else if (!W_ERROR_IS_OK(r.out.result)) {
228                 printf("DsWriteAccountSpn failed - %s\n", win_errstr(r.out.result));
229                 ret = false;
230         }
231
232         return ret;
233 }
234
235 static bool test_DsReplicaGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
236                         struct DsPrivate *priv)
237 {
238         NTSTATUS status;
239         struct drsuapi_DsReplicaGetInfo r;
240         bool ret = true;
241         int i;
242         struct {
243                 int32_t level;
244                 int32_t infotype;
245                 const char *obj_dn;
246         } array[] = {
247                 {       
248                         DRSUAPI_DS_REPLICA_GET_INFO,
249                         DRSUAPI_DS_REPLICA_INFO_NEIGHBORS,
250                         NULL
251                 },{
252                         DRSUAPI_DS_REPLICA_GET_INFO,
253                         DRSUAPI_DS_REPLICA_INFO_CURSORS,
254                         NULL
255                 },{
256                         DRSUAPI_DS_REPLICA_GET_INFO,
257                         DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA,
258                         NULL
259                 },{
260                         DRSUAPI_DS_REPLICA_GET_INFO,
261                         DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES,
262                         NULL
263                 },{
264                         DRSUAPI_DS_REPLICA_GET_INFO,
265                         DRSUAPI_DS_REPLICA_INFO_KCC_DSA_LINK_FAILURES,
266                         NULL
267                 },{
268                         DRSUAPI_DS_REPLICA_GET_INFO,
269                         DRSUAPI_DS_REPLICA_INFO_PENDING_OPS,
270                         NULL
271                 },{
272                         DRSUAPI_DS_REPLICA_GET_INFO2,
273                         DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA,
274                         NULL
275                 },{
276                         DRSUAPI_DS_REPLICA_GET_INFO2,
277                         DRSUAPI_DS_REPLICA_INFO_CURSORS2,
278                         NULL
279                 },{
280                         DRSUAPI_DS_REPLICA_GET_INFO2,
281                         DRSUAPI_DS_REPLICA_INFO_CURSORS3,
282                         NULL
283                 },{
284                         DRSUAPI_DS_REPLICA_GET_INFO2,
285                         DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA2,
286                         NULL
287                 },{
288                         DRSUAPI_DS_REPLICA_GET_INFO2,
289                         DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA2,
290                         NULL
291                 },{
292                         DRSUAPI_DS_REPLICA_GET_INFO2,
293                         DRSUAPI_DS_REPLICA_INFO_NEIGHBORS02,
294                         NULL
295                 },{
296                         DRSUAPI_DS_REPLICA_GET_INFO2,
297                         DRSUAPI_DS_REPLICA_INFO_CONNECTIONS04,
298                         "__IGNORED__"
299                 },{
300                         DRSUAPI_DS_REPLICA_GET_INFO2,
301                         DRSUAPI_DS_REPLICA_INFO_CURSORS05,
302                         NULL
303                 },{
304                         DRSUAPI_DS_REPLICA_GET_INFO2,
305                         DRSUAPI_DS_REPLICA_INFO_06,
306                         NULL
307                 }
308         };
309
310         if (lp_parm_bool(global_loadparm, NULL, "torture", "samba4", false)) {
311                 printf("skipping DsReplicaGetInfo test against Samba4\n");
312                 return true;
313         }
314
315         r.in.bind_handle        = &priv->bind_handle;
316
317         for (i=0; i < ARRAY_SIZE(array); i++) {
318                 const char *object_dn;
319
320                 printf("testing DsReplicaGetInfo level %d infotype %d\n",
321                         array[i].level, array[i].infotype);
322
323                 object_dn = (array[i].obj_dn ? array[i].obj_dn : priv->domain_obj_dn);
324
325                 r.in.level = array[i].level;
326                 switch(r.in.level) {
327                 case DRSUAPI_DS_REPLICA_GET_INFO:
328                         r.in.req.req1.info_type = array[i].infotype;
329                         r.in.req.req1.object_dn = object_dn;
330                         ZERO_STRUCT(r.in.req.req1.guid1);
331                         break;
332                 case DRSUAPI_DS_REPLICA_GET_INFO2:
333                         r.in.req.req2.info_type = array[i].infotype;
334                         r.in.req.req2.object_dn = object_dn;
335                         ZERO_STRUCT(r.in.req.req1.guid1);
336                         r.in.req.req2.unknown1  = 0;
337                         r.in.req.req2.string1   = NULL;
338                         r.in.req.req2.string2   = NULL;
339                         r.in.req.req2.unknown2  = 0;
340                         break;
341                 }
342
343                 status = dcerpc_drsuapi_DsReplicaGetInfo(p, mem_ctx, &r);
344                 if (!NT_STATUS_IS_OK(status)) {
345                         const char *errstr = nt_errstr(status);
346                         if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
347                                 errstr = dcerpc_errstr(mem_ctx, p->last_fault_code);
348                         }
349                         if (p->last_fault_code != DCERPC_FAULT_INVALID_TAG) {
350                                 printf("dcerpc_drsuapi_DsReplicaGetInfo failed - %s\n", errstr);
351                                 ret = false;
352                         } else {
353                                 printf("DsReplicaGetInfo level %d and/or infotype %d not supported by server\n",
354                                         array[i].level, array[i].infotype);
355                         }
356                 } else if (!W_ERROR_IS_OK(r.out.result)) {
357                         printf("DsReplicaGetInfo failed - %s\n", win_errstr(r.out.result));
358                         ret = false;
359                 }
360         }
361
362         return ret;
363 }
364
365 static bool test_DsReplicaSync(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
366                         struct DsPrivate *priv)
367 {
368         NTSTATUS status;
369         bool ret = true;
370         int i;
371         struct drsuapi_DsReplicaSync r;
372         struct drsuapi_DsReplicaObjectIdentifier nc;
373         struct GUID null_guid;
374         struct dom_sid null_sid;
375         struct {
376                 int32_t level;
377         } array[] = {
378                 {       
379                         1
380                 }
381         };
382
383         if (!lp_parm_bool(global_loadparm, NULL, "torture", "dangerous", false)) {
384                 printf("DsReplicaSync disabled - enable dangerous tests to use\n");
385                 return true;
386         }
387
388         if (lp_parm_bool(global_loadparm, NULL, "torture", "samba4", false)) {
389                 printf("skipping DsReplicaSync test against Samba4\n");
390                 return true;
391         }
392
393         ZERO_STRUCT(null_guid);
394         ZERO_STRUCT(null_sid);
395
396         r.in.bind_handle        = &priv->bind_handle;
397
398         for (i=0; i < ARRAY_SIZE(array); i++) {
399                 printf("testing DsReplicaSync level %d\n",
400                         array[i].level);
401
402                 r.in.level = array[i].level;
403                 switch(r.in.level) {
404                 case 1:
405                         nc.guid                                 = null_guid;
406                         nc.sid                                  = null_sid;
407                         nc.dn                                   = priv->domain_obj_dn?priv->domain_obj_dn:"";
408
409                         r.in.req.req1.naming_context            = &nc;
410                         r.in.req.req1.source_dsa_guid           = priv->dcinfo.ntds_guid;
411                         r.in.req.req1.other_info                = NULL;
412                         r.in.req.req1.options                   = 16;
413                         break;
414                 }
415
416                 status = dcerpc_drsuapi_DsReplicaSync(p, mem_ctx, &r);
417                 if (!NT_STATUS_IS_OK(status)) {
418                         const char *errstr = nt_errstr(status);
419                         if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
420                                 errstr = dcerpc_errstr(mem_ctx, p->last_fault_code);
421                         }
422                         printf("dcerpc_drsuapi_DsReplicaSync failed - %s\n", errstr);
423                         ret = false;
424                 } else if (!W_ERROR_IS_OK(r.out.result)) {
425                         printf("DsReplicaSync failed - %s\n", win_errstr(r.out.result));
426                         ret = false;
427                 }
428         }
429
430         return ret;
431 }
432
433 static bool test_DsReplicaUpdateRefs(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
434                         struct DsPrivate *priv)
435 {
436         NTSTATUS status;
437         bool ret = true;
438         int i;
439         struct drsuapi_DsReplicaUpdateRefs r;
440         struct drsuapi_DsReplicaObjectIdentifier nc;
441         struct GUID null_guid;
442         struct dom_sid null_sid;
443         struct {
444                 int32_t level;
445         } array[] = {
446                 {       
447                         1
448                 }
449         };
450
451         if (lp_parm_bool(global_loadparm, NULL, "torture", "samba4", false)) {
452                 printf("skipping DsReplicaUpdateRefs test against Samba4\n");
453                 return true;
454         }
455
456         ZERO_STRUCT(null_guid);
457         ZERO_STRUCT(null_sid);
458
459         r.in.bind_handle        = &priv->bind_handle;
460
461         for (i=0; i < ARRAY_SIZE(array); i++) {
462                 printf("testing DsReplicaUpdateRefs level %d\n",
463                         array[i].level);
464
465                 r.in.level = array[i].level;
466                 switch(r.in.level) {
467                 case 1:
468                         nc.guid                         = null_guid;
469                         nc.sid                          = null_sid;
470                         nc.dn                           = priv->domain_obj_dn?priv->domain_obj_dn:"";
471
472                         r.in.req.req1.naming_context    = &nc;
473                         r.in.req.req1.dest_dsa_dns_name = talloc_asprintf(mem_ctx, "__some_dest_dsa_guid_string._msdn.%s",
474                                                                                 priv->domain_dns_name);
475                         r.in.req.req1.dest_dsa_guid     = null_guid;
476                         r.in.req.req1.options           = 0;
477                         break;
478                 }
479
480                 status = dcerpc_drsuapi_DsReplicaUpdateRefs(p, mem_ctx, &r);
481                 if (!NT_STATUS_IS_OK(status)) {
482                         const char *errstr = nt_errstr(status);
483                         if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
484                                 errstr = dcerpc_errstr(mem_ctx, p->last_fault_code);
485                         }
486                         printf("dcerpc_drsuapi_DsReplicaUpdateRefs failed - %s\n", errstr);
487                         ret = false;
488                 } else if (!W_ERROR_IS_OK(r.out.result)) {
489                         printf("DsReplicaUpdateRefs failed - %s\n", win_errstr(r.out.result));
490                         ret = false;
491                 }
492         }
493
494         return ret;
495 }
496
497 static bool test_DsGetNCChanges(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
498                         struct DsPrivate *priv)
499 {
500         NTSTATUS status;
501         bool ret = true;
502         int i;
503         struct drsuapi_DsGetNCChanges r;
504         struct drsuapi_DsReplicaObjectIdentifier nc;
505         struct GUID null_guid;
506         struct dom_sid null_sid;
507         struct {
508                 int32_t level;
509         } array[] = {
510                 {       
511                         5
512                 },
513                 {       
514                         8
515                 }
516         };
517
518         if (lp_parm_bool(global_loadparm, NULL, "torture", "samba4", false)) {
519                 printf("skipping DsGetNCChanges test against Samba4\n");
520                 return true;
521         }
522
523         ZERO_STRUCT(null_guid);
524         ZERO_STRUCT(null_sid);
525
526         for (i=0; i < ARRAY_SIZE(array); i++) {
527                 printf("testing DsGetNCChanges level %d\n",
528                         array[i].level);
529
530                 r.in.bind_handle        = &priv->bind_handle;
531                 r.in.level              = &array[i].level;
532
533                 switch (*r.in.level) {
534                 case 5:
535                         nc.guid = null_guid;
536                         nc.sid  = null_sid;
537                         nc.dn   = priv->domain_obj_dn?priv->domain_obj_dn:"";
538
539                         r.in.req.req5.destination_dsa_guid              = GUID_random();
540                         r.in.req.req5.source_dsa_invocation_id          = null_guid;
541                         r.in.req.req5.naming_context                    = &nc;
542                         r.in.req.req5.highwatermark.tmp_highest_usn     = 0;
543                         r.in.req.req5.highwatermark.reserved_usn        = 0;
544                         r.in.req.req5.highwatermark.highest_usn         = 0;
545                         r.in.req.req5.uptodateness_vector               = NULL;
546                         r.in.req.req5.replica_flags                     = 0;
547                         if (lp_parm_bool(global_loadparm, NULL, "drsuapi","compression", false)) {
548                                 r.in.req.req5.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
549                         }
550                         r.in.req.req5.max_object_count                  = 0;
551                         r.in.req.req5.max_ndr_size                      = 0;
552                         r.in.req.req5.unknown4                          = 0;
553                         r.in.req.req5.h1                                = 0;
554
555                         break;
556                 case 8:
557                         nc.guid = null_guid;
558                         nc.sid  = null_sid;
559                         nc.dn   = priv->domain_obj_dn?priv->domain_obj_dn:"";
560
561                         r.in.req.req8.destination_dsa_guid              = GUID_random();
562                         r.in.req.req8.source_dsa_invocation_id          = null_guid;
563                         r.in.req.req8.naming_context                    = &nc;
564                         r.in.req.req8.highwatermark.tmp_highest_usn     = 0;
565                         r.in.req.req8.highwatermark.reserved_usn        = 0;
566                         r.in.req.req8.highwatermark.highest_usn         = 0;
567                         r.in.req.req8.uptodateness_vector               = NULL;
568                         r.in.req.req8.replica_flags                     = 0;
569                         if (lp_parm_bool(global_loadparm, NULL, "drsuapi", "compression", false)) {
570                                 r.in.req.req8.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
571                         }
572                         if (lp_parm_bool(global_loadparm, NULL, "drsuapi", "neighbour_writeable", true)) {
573                                 r.in.req.req8.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
574                         }
575                         r.in.req.req8.replica_flags                     |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
576                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
577                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
578                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
579                                                                         ;
580                         r.in.req.req8.max_object_count                  = 402;
581                         r.in.req.req8.max_ndr_size                      = 402116;
582                         r.in.req.req8.unknown4                          = 0;
583                         r.in.req.req8.h1                                = 0;
584                         r.in.req.req8.unique_ptr1                       = 0;
585                         r.in.req.req8.unique_ptr2                       = 0;
586                         r.in.req.req8.mapping_ctr.num_mappings          = 0;
587                         r.in.req.req8.mapping_ctr.mappings              = NULL;
588
589                         break;
590                 }
591
592                 status = dcerpc_drsuapi_DsGetNCChanges(p, mem_ctx, &r);
593                 if (!NT_STATUS_IS_OK(status)) {
594                         const char *errstr = nt_errstr(status);
595                         if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
596                                 errstr = dcerpc_errstr(mem_ctx, p->last_fault_code);
597                         }
598                         printf("dcerpc_drsuapi_DsGetNCChanges failed - %s\n", errstr);
599                         ret = false;
600                 } else if (!W_ERROR_IS_OK(r.out.result)) {
601                         printf("DsGetNCChanges failed - %s\n", win_errstr(r.out.result));
602                         ret = false;
603                 }
604         }
605
606         return ret;
607 }
608
609 bool test_QuerySitesByCost(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
610                            struct DsPrivate *priv)
611 {
612         NTSTATUS status;
613         struct drsuapi_QuerySitesByCost r;
614         bool ret = true;
615
616         const char *my_site = "Default-First-Site-Name";
617         const char *remote_site1 = "smbtorture-nonexisting-site1";
618         const char *remote_site2 = "smbtorture-nonexisting-site2";
619
620         r.in.bind_handle = &priv->bind_handle;
621         r.in.level = 1;
622         r.in.req.req1.site_from = talloc_strdup(mem_ctx, my_site);
623         r.in.req.req1.num_req = 2;
624         r.in.req.req1.site_to = talloc_zero_array(mem_ctx, const char *, r.in.req.req1.num_req);
625         r.in.req.req1.site_to[0] = talloc_strdup(mem_ctx, remote_site1);
626         r.in.req.req1.site_to[1] = talloc_strdup(mem_ctx, remote_site2);
627         r.in.req.req1.flags = 0;
628
629         status = dcerpc_drsuapi_QuerySitesByCost(p, mem_ctx, &r);
630         if (!NT_STATUS_IS_OK(status)) {
631                 const char *errstr = nt_errstr(status);
632                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
633                         errstr = dcerpc_errstr(mem_ctx, p->last_fault_code);
634                 }
635                 printf("drsuapi_QuerySitesByCost - %s\n", errstr);
636                 ret = false;
637         } else if (!W_ERROR_IS_OK(r.out.result)) {
638                 printf("QuerySitesByCost failed - %s\n", win_errstr(r.out.result));
639                 ret = false;
640         }
641
642         if (W_ERROR_IS_OK(r.out.result)) {
643
644                 if (!W_ERROR_EQUAL(r.out.ctr.ctr1.info[0].error_code, WERR_DS_OBJ_NOT_FOUND) ||
645                     !W_ERROR_EQUAL(r.out.ctr.ctr1.info[1].error_code, WERR_DS_OBJ_NOT_FOUND)) { 
646                         printf("expected error_code WERR_DS_OBJ_NOT_FOUND, got %s\n", 
647                                 win_errstr(r.out.ctr.ctr1.info[0].error_code));
648                         ret = false;
649                 }
650
651                 if ((r.out.ctr.ctr1.info[0].site_cost != (uint32_t) -1) ||
652                     (r.out.ctr.ctr1.info[1].site_cost != (uint32_t) -1)) {
653                         printf("expected site_cost %d, got %d\n", 
654                                 (uint32_t) -1, r.out.ctr.ctr1.info[0].site_cost);
655                         ret = false;
656                 }
657         }
658
659         return ret;
660
661
662 }
663
664 bool test_DsUnbind(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
665                    struct DsPrivate *priv)
666 {
667         NTSTATUS status;
668         struct drsuapi_DsUnbind r;
669         bool ret = true;
670
671         r.in.bind_handle = &priv->bind_handle;
672         r.out.bind_handle = &priv->bind_handle;
673
674         printf("testing DsUnbind\n");
675
676         status = dcerpc_drsuapi_DsUnbind(p, mem_ctx, &r);
677         if (!NT_STATUS_IS_OK(status)) {
678                 const char *errstr = nt_errstr(status);
679                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
680                         errstr = dcerpc_errstr(mem_ctx, p->last_fault_code);
681                 }
682                 printf("dcerpc_drsuapi_DsUnbind failed - %s\n", errstr);
683                 ret = false;
684         } else if (!W_ERROR_IS_OK(r.out.result)) {
685                 printf("DsBind failed - %s\n", win_errstr(r.out.result));
686                 ret = false;
687         }
688
689         return ret;
690 }
691
692 bool torture_rpc_drsuapi(struct torture_context *torture)
693 {
694         NTSTATUS status;
695         struct dcerpc_pipe *p;
696         bool ret = true;
697         struct DsPrivate priv;
698         struct cli_credentials *machine_credentials;
699
700         ZERO_STRUCT(priv);
701
702         priv.join = torture_join_domain(TEST_MACHINE_NAME, ACB_SVRTRUST, 
703                                        &machine_credentials);
704         if (!priv.join) {
705                 torture_fail(torture, "Failed to join as BDC");
706         }
707
708         status = torture_rpc_connection(torture, 
709                                         &p, 
710                                         &ndr_table_drsuapi);
711         if (!NT_STATUS_IS_OK(status)) {
712                 torture_leave_domain(priv.join);
713                 torture_fail(torture, "Unable to connect to DRSUAPI pipe");
714         }
715
716         ret &= test_DsBind(p, torture, &priv);
717 #if 0
718         ret &= test_QuerySitesByCost(p, torture, &priv);
719 #endif
720         ret &= test_DsGetDomainControllerInfo(p, torture, &priv);
721
722         ret &= test_DsCrackNames(p, torture, &priv);
723
724         ret &= test_DsWriteAccountSpn(p, torture, &priv);
725
726         ret &= test_DsReplicaGetInfo(p, torture, &priv);
727
728         ret &= test_DsReplicaSync(p, torture, &priv);
729
730         ret &= test_DsReplicaUpdateRefs(p, torture, &priv);
731
732         ret &= test_DsGetNCChanges(p, torture, &priv);
733
734         ret &= test_DsUnbind(p, torture, &priv);
735
736         torture_leave_domain(priv.join);
737
738         return ret;
739 }
740
741
742 bool torture_rpc_drsuapi_cracknames(struct torture_context *torture)
743 {
744         NTSTATUS status;
745         struct dcerpc_pipe *p;
746         bool ret = true;
747         struct DsPrivate priv;
748         struct cli_credentials *machine_credentials;
749
750         torture_comment(torture, "Connected to DRSUAPI pipe\n");
751
752         ZERO_STRUCT(priv);
753
754         priv.join = torture_join_domain(TEST_MACHINE_NAME, ACB_SVRTRUST, 
755                                        &machine_credentials);
756         if (!priv.join) {
757                 torture_fail(torture, "Failed to join as BDC\n");
758         }
759
760         status = torture_rpc_connection(torture, 
761                                         &p, 
762                                         &ndr_table_drsuapi);
763         if (!NT_STATUS_IS_OK(status)) {
764                 torture_leave_domain(priv.join);
765                 torture_fail(torture, "Unable to connect to DRSUAPI pipe");
766         }
767
768         ret &= test_DsBind(p, torture, &priv);
769
770         if (ret) {
771                 /* We don't care if this fails, we just need some info from it */
772                 test_DsGetDomainControllerInfo(p, torture, &priv);
773                 
774                 ret &= test_DsCrackNames(p, torture, &priv);
775                 
776                 ret &= test_DsUnbind(p, torture, &priv);
777         }
778
779         torture_leave_domain(priv.join);
780
781         return ret;
782 }
783