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