s4: merge from s3 drsuapi.idl.
[abartlet/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 #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, struct torture_context *tctx,
236                         struct DsPrivate *priv)
237 {
238         NTSTATUS status;
239         struct drsuapi_DsReplicaGetInfo r;
240         union drsuapi_DsReplicaGetInfoRequest req;
241         bool ret = true;
242         int i;
243         struct {
244                 int32_t level;
245                 int32_t infotype;
246                 const char *obj_dn;
247         } array[] = {
248                 {       
249                         DRSUAPI_DS_REPLICA_GET_INFO,
250                         DRSUAPI_DS_REPLICA_INFO_NEIGHBORS,
251                         NULL
252                 },{
253                         DRSUAPI_DS_REPLICA_GET_INFO,
254                         DRSUAPI_DS_REPLICA_INFO_CURSORS,
255                         NULL
256                 },{
257                         DRSUAPI_DS_REPLICA_GET_INFO,
258                         DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA,
259                         NULL
260                 },{
261                         DRSUAPI_DS_REPLICA_GET_INFO,
262                         DRSUAPI_DS_REPLICA_INFO_KCC_DSA_CONNECT_FAILURES,
263                         NULL
264                 },{
265                         DRSUAPI_DS_REPLICA_GET_INFO,
266                         DRSUAPI_DS_REPLICA_INFO_KCC_DSA_LINK_FAILURES,
267                         NULL
268                 },{
269                         DRSUAPI_DS_REPLICA_GET_INFO,
270                         DRSUAPI_DS_REPLICA_INFO_PENDING_OPS,
271                         NULL
272                 },{
273                         DRSUAPI_DS_REPLICA_GET_INFO2,
274                         DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA,
275                         NULL
276                 },{
277                         DRSUAPI_DS_REPLICA_GET_INFO2,
278                         DRSUAPI_DS_REPLICA_INFO_CURSORS2,
279                         NULL
280                 },{
281                         DRSUAPI_DS_REPLICA_GET_INFO2,
282                         DRSUAPI_DS_REPLICA_INFO_CURSORS3,
283                         NULL
284                 },{
285                         DRSUAPI_DS_REPLICA_GET_INFO2,
286                         DRSUAPI_DS_REPLICA_INFO_OBJ_METADATA2,
287                         NULL
288                 },{
289                         DRSUAPI_DS_REPLICA_GET_INFO2,
290                         DRSUAPI_DS_REPLICA_INFO_ATTRIBUTE_VALUE_METADATA2,
291                         NULL
292                 },{
293                         DRSUAPI_DS_REPLICA_GET_INFO2,
294                         DRSUAPI_DS_REPLICA_INFO_NEIGHBORS02,
295                         NULL
296                 },{
297                         DRSUAPI_DS_REPLICA_GET_INFO2,
298                         DRSUAPI_DS_REPLICA_INFO_CONNECTIONS04,
299                         "__IGNORED__"
300                 },{
301                         DRSUAPI_DS_REPLICA_GET_INFO2,
302                         DRSUAPI_DS_REPLICA_INFO_CURSORS05,
303                         NULL
304                 },{
305                         DRSUAPI_DS_REPLICA_GET_INFO2,
306                         DRSUAPI_DS_REPLICA_INFO_06,
307                         NULL
308                 }
309         };
310
311         if (torture_setting_bool(tctx, "samba4", false)) {
312                 printf("skipping DsReplicaGetInfo test against Samba4\n");
313                 return true;
314         }
315
316         r.in.bind_handle        = &priv->bind_handle;
317
318         for (i=0; i < ARRAY_SIZE(array); i++) {
319                 const char *object_dn;
320
321                 printf("testing DsReplicaGetInfo level %d infotype %d\n",
322                         array[i].level, array[i].infotype);
323
324                 object_dn = (array[i].obj_dn ? array[i].obj_dn : priv->domain_obj_dn);
325
326                 r.in.level = array[i].level;
327                 switch(r.in.level) {
328                 case DRSUAPI_DS_REPLICA_GET_INFO:
329                         req.req1.info_type      = array[i].infotype;
330                         req.req1.object_dn      = object_dn;
331                         ZERO_STRUCT(req.req1.guid1);
332                         break;
333                 case DRSUAPI_DS_REPLICA_GET_INFO2:
334                         req.req2.info_type      = array[i].infotype;
335                         req.req2.object_dn      = object_dn;
336                         ZERO_STRUCT(req.req1.guid1);
337                         req.req2.unknown1       = 0;
338                         req.req2.string1        = NULL;
339                         req.req2.string2        = NULL;
340                         req.req2.unknown2       = 0;
341                         break;
342                 }
343                 r.in.req = &req;
344
345                 status = dcerpc_drsuapi_DsReplicaGetInfo(p, tctx, &r);
346                 if (!NT_STATUS_IS_OK(status)) {
347                         const char *errstr = nt_errstr(status);
348                         if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
349                                 errstr = dcerpc_errstr(tctx, p->last_fault_code);
350                         }
351                         if (p->last_fault_code != DCERPC_FAULT_INVALID_TAG) {
352                                 printf("dcerpc_drsuapi_DsReplicaGetInfo failed - %s\n", errstr);
353                                 ret = false;
354                         } else {
355                                 printf("DsReplicaGetInfo level %d and/or infotype %d not supported by server\n",
356                                         array[i].level, array[i].infotype);
357                         }
358                 } else if (!W_ERROR_IS_OK(r.out.result)) {
359                         printf("DsReplicaGetInfo failed - %s\n", win_errstr(r.out.result));
360                         ret = false;
361                 }
362         }
363
364         return ret;
365 }
366
367 static bool test_DsReplicaSync(struct dcerpc_pipe *p, struct torture_context *tctx,
368                         struct DsPrivate *priv)
369 {
370         NTSTATUS status;
371         bool ret = true;
372         int i;
373         struct drsuapi_DsReplicaSync r;
374         struct drsuapi_DsReplicaObjectIdentifier nc;
375         struct GUID null_guid;
376         struct dom_sid null_sid;
377         struct {
378                 int32_t level;
379         } array[] = {
380                 {       
381                         1
382                 }
383         };
384
385         if (!torture_setting_bool(tctx, "dangerous", false)) {
386                 printf("DsReplicaSync disabled - enable dangerous tests to use\n");
387                 return true;
388         }
389
390         if (torture_setting_bool(tctx, "samba4", false)) {
391                 printf("skipping DsReplicaSync test against Samba4\n");
392                 return true;
393         }
394
395         ZERO_STRUCT(null_guid);
396         ZERO_STRUCT(null_sid);
397
398         r.in.bind_handle        = &priv->bind_handle;
399
400         for (i=0; i < ARRAY_SIZE(array); i++) {
401                 printf("testing DsReplicaSync level %d\n",
402                         array[i].level);
403
404                 r.in.level = array[i].level;
405                 switch(r.in.level) {
406                 case 1:
407                         nc.guid                                 = null_guid;
408                         nc.sid                                  = null_sid;
409                         nc.dn                                   = priv->domain_obj_dn?priv->domain_obj_dn:"";
410
411                         r.in.req.req1.naming_context            = &nc;
412                         r.in.req.req1.source_dsa_guid           = priv->dcinfo.ntds_guid;
413                         r.in.req.req1.other_info                = NULL;
414                         r.in.req.req1.options                   = 16;
415                         break;
416                 }
417
418                 status = dcerpc_drsuapi_DsReplicaSync(p, tctx, &r);
419                 if (!NT_STATUS_IS_OK(status)) {
420                         const char *errstr = nt_errstr(status);
421                         if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
422                                 errstr = dcerpc_errstr(tctx, p->last_fault_code);
423                         }
424                         printf("dcerpc_drsuapi_DsReplicaSync failed - %s\n", errstr);
425                         ret = false;
426                 } else if (!W_ERROR_IS_OK(r.out.result)) {
427                         printf("DsReplicaSync failed - %s\n", win_errstr(r.out.result));
428                         ret = false;
429                 }
430         }
431
432         return ret;
433 }
434
435 static bool test_DsReplicaUpdateRefs(struct dcerpc_pipe *p, struct torture_context *tctx,
436                         struct DsPrivate *priv)
437 {
438         NTSTATUS status;
439         bool ret = true;
440         int i;
441         struct drsuapi_DsReplicaUpdateRefs r;
442         struct drsuapi_DsReplicaObjectIdentifier nc;
443         struct GUID null_guid;
444         struct dom_sid null_sid;
445         struct {
446                 int32_t level;
447         } array[] = {
448                 {       
449                         1
450                 }
451         };
452
453         if (torture_setting_bool(tctx, "samba4", false)) {
454                 printf("skipping DsReplicaUpdateRefs test against Samba4\n");
455                 return true;
456         }
457
458         ZERO_STRUCT(null_guid);
459         ZERO_STRUCT(null_sid);
460
461         r.in.bind_handle        = &priv->bind_handle;
462
463         for (i=0; i < ARRAY_SIZE(array); i++) {
464                 printf("testing DsReplicaUpdateRefs level %d\n",
465                         array[i].level);
466
467                 r.in.level = array[i].level;
468                 switch(r.in.level) {
469                 case 1:
470                         nc.guid                         = null_guid;
471                         nc.sid                          = null_sid;
472                         nc.dn                           = priv->domain_obj_dn?priv->domain_obj_dn:"";
473
474                         r.in.req.req1.naming_context    = &nc;
475                         r.in.req.req1.dest_dsa_dns_name = talloc_asprintf(tctx, "__some_dest_dsa_guid_string._msdn.%s",
476                                                                                 priv->domain_dns_name);
477                         r.in.req.req1.dest_dsa_guid     = null_guid;
478                         r.in.req.req1.options           = 0;
479                         break;
480                 }
481
482                 status = dcerpc_drsuapi_DsReplicaUpdateRefs(p, tctx, &r);
483                 if (!NT_STATUS_IS_OK(status)) {
484                         const char *errstr = nt_errstr(status);
485                         if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
486                                 errstr = dcerpc_errstr(tctx, p->last_fault_code);
487                         }
488                         printf("dcerpc_drsuapi_DsReplicaUpdateRefs failed - %s\n", errstr);
489                         ret = false;
490                 } else if (!W_ERROR_IS_OK(r.out.result)) {
491                         printf("DsReplicaUpdateRefs failed - %s\n", win_errstr(r.out.result));
492                         ret = false;
493                 }
494         }
495
496         return ret;
497 }
498
499 static bool test_DsGetNCChanges(struct dcerpc_pipe *p, struct torture_context *tctx, 
500                         struct DsPrivate *priv)
501 {
502         NTSTATUS status;
503         bool ret = true;
504         int i;
505         struct drsuapi_DsGetNCChanges r;
506         struct drsuapi_DsReplicaObjectIdentifier nc;
507         struct GUID null_guid;
508         struct dom_sid null_sid;
509         struct {
510                 int32_t level;
511         } array[] = {
512                 {       
513                         5
514                 },
515                 {       
516                         8
517                 }
518         };
519
520         if (torture_setting_bool(tctx, "samba4", false)) {
521                 printf("skipping DsGetNCChanges test against Samba4\n");
522                 return true;
523         }
524
525         ZERO_STRUCT(null_guid);
526         ZERO_STRUCT(null_sid);
527
528         for (i=0; i < ARRAY_SIZE(array); i++) {
529                 printf("testing DsGetNCChanges level %d\n",
530                         array[i].level);
531
532                 r.in.bind_handle        = &priv->bind_handle;
533                 r.in.level              = &array[i].level;
534
535                 switch (*r.in.level) {
536                 case 5:
537                         nc.guid = null_guid;
538                         nc.sid  = null_sid;
539                         nc.dn   = priv->domain_obj_dn?priv->domain_obj_dn:"";
540
541                         r.in.req.req5.destination_dsa_guid              = GUID_random();
542                         r.in.req.req5.source_dsa_invocation_id          = null_guid;
543                         r.in.req.req5.naming_context                    = &nc;
544                         r.in.req.req5.highwatermark.tmp_highest_usn     = 0;
545                         r.in.req.req5.highwatermark.reserved_usn        = 0;
546                         r.in.req.req5.highwatermark.highest_usn         = 0;
547                         r.in.req.req5.uptodateness_vector               = NULL;
548                         r.in.req.req5.replica_flags                     = 0;
549                         if (lp_parm_bool(tctx->lp_ctx, NULL, "drsuapi","compression", false)) {
550                                 r.in.req.req5.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
551                         }
552                         r.in.req.req5.max_object_count                  = 0;
553                         r.in.req.req5.max_ndr_size                      = 0;
554                         r.in.req.req5.extended_op                       = DRSUAPI_EXOP_NONE;
555                         r.in.req.req5.fsmo_info                         = 0;
556
557                         break;
558                 case 8:
559                         nc.guid = null_guid;
560                         nc.sid  = null_sid;
561                         nc.dn   = priv->domain_obj_dn?priv->domain_obj_dn:"";
562
563                         r.in.req.req8.destination_dsa_guid              = GUID_random();
564                         r.in.req.req8.source_dsa_invocation_id          = null_guid;
565                         r.in.req.req8.naming_context                    = &nc;
566                         r.in.req.req8.highwatermark.tmp_highest_usn     = 0;
567                         r.in.req.req8.highwatermark.reserved_usn        = 0;
568                         r.in.req.req8.highwatermark.highest_usn         = 0;
569                         r.in.req.req8.uptodateness_vector               = NULL;
570                         r.in.req.req8.replica_flags                     = 0;
571                         if (lp_parm_bool(tctx->lp_ctx, NULL, "drsuapi", "compression", false)) {
572                                 r.in.req.req8.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
573                         }
574                         if (lp_parm_bool(tctx->lp_ctx, NULL, "drsuapi", "neighbour_writeable", true)) {
575                                 r.in.req.req8.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
576                         }
577                         r.in.req.req8.replica_flags                     |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
578                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
579                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
580                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
581                                                                         ;
582                         r.in.req.req8.max_object_count                  = 402;
583                         r.in.req.req8.max_ndr_size                      = 402116;
584                         r.in.req.req8.extended_op                       = DRSUAPI_EXOP_NONE;
585                         r.in.req.req8.fsmo_info                         = 0;
586                         r.in.req.req8.partial_attribute_set             = NULL;
587                         r.in.req.req8.partial_attribute_set_ex          = NULL;
588                         r.in.req.req8.mapping_ctr.num_mappings          = 0;
589                         r.in.req.req8.mapping_ctr.mappings              = NULL;
590
591                         break;
592                 }
593
594                 status = dcerpc_drsuapi_DsGetNCChanges(p, tctx, &r);
595                 if (!NT_STATUS_IS_OK(status)) {
596                         const char *errstr = nt_errstr(status);
597                         if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
598                                 errstr = dcerpc_errstr(tctx, p->last_fault_code);
599                         }
600                         printf("dcerpc_drsuapi_DsGetNCChanges failed - %s\n", errstr);
601                         ret = false;
602                 } else if (!W_ERROR_IS_OK(r.out.result)) {
603                         printf("DsGetNCChanges failed - %s\n", win_errstr(r.out.result));
604                         ret = false;
605                 }
606         }
607
608         return ret;
609 }
610
611 bool test_QuerySitesByCost(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
612                            struct DsPrivate *priv)
613 {
614         NTSTATUS status;
615         struct drsuapi_QuerySitesByCost r;
616         union drsuapi_QuerySitesByCostRequest req;
617         bool ret = true;
618
619         const char *my_site = "Default-First-Site-Name";
620         const char *remote_site1 = "smbtorture-nonexisting-site1";
621         const char *remote_site2 = "smbtorture-nonexisting-site2";
622
623         req.req1.site_from = talloc_strdup(mem_ctx, my_site);
624         req.req1.num_req = 2;
625         req.req1.site_to = talloc_zero_array(mem_ctx, const char *, 2);
626         req.req1.site_to[0] = talloc_strdup(mem_ctx, remote_site1);
627         req.req1.site_to[1] = talloc_strdup(mem_ctx, remote_site2);
628         req.req1.flags = 0;
629
630         r.in.bind_handle = &priv->bind_handle;
631         r.in.level = 1;
632         r.in.req = &req;
633
634         status = dcerpc_drsuapi_QuerySitesByCost(p, mem_ctx, &r);
635         if (!NT_STATUS_IS_OK(status)) {
636                 const char *errstr = nt_errstr(status);
637                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
638                         errstr = dcerpc_errstr(mem_ctx, p->last_fault_code);
639                 }
640                 printf("drsuapi_QuerySitesByCost - %s\n", errstr);
641                 ret = false;
642         } else if (!W_ERROR_IS_OK(r.out.result)) {
643                 printf("QuerySitesByCost failed - %s\n", win_errstr(r.out.result));
644                 ret = false;
645         }
646
647         if (W_ERROR_IS_OK(r.out.result)) {
648
649                 if (!W_ERROR_EQUAL(r.out.ctr->ctr1.info[0].error_code, WERR_DS_OBJ_NOT_FOUND) ||
650                     !W_ERROR_EQUAL(r.out.ctr->ctr1.info[1].error_code, WERR_DS_OBJ_NOT_FOUND)) {
651                         printf("expected error_code WERR_DS_OBJ_NOT_FOUND, got %s\n", 
652                                 win_errstr(r.out.ctr->ctr1.info[0].error_code));
653                         ret = false;
654                 }
655
656                 if ((r.out.ctr->ctr1.info[0].site_cost != (uint32_t) -1) ||
657                     (r.out.ctr->ctr1.info[1].site_cost != (uint32_t) -1)) {
658                         printf("expected site_cost %d, got %d\n", 
659                                 (uint32_t) -1, r.out.ctr->ctr1.info[0].site_cost);
660                         ret = false;
661                 }
662         }
663
664         return ret;
665
666
667 }
668
669 bool test_DsUnbind(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
670                    struct DsPrivate *priv)
671 {
672         NTSTATUS status;
673         struct drsuapi_DsUnbind r;
674         bool ret = true;
675
676         r.in.bind_handle = &priv->bind_handle;
677         r.out.bind_handle = &priv->bind_handle;
678
679         printf("testing DsUnbind\n");
680
681         status = dcerpc_drsuapi_DsUnbind(p, mem_ctx, &r);
682         if (!NT_STATUS_IS_OK(status)) {
683                 const char *errstr = nt_errstr(status);
684                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
685                         errstr = dcerpc_errstr(mem_ctx, p->last_fault_code);
686                 }
687                 printf("dcerpc_drsuapi_DsUnbind failed - %s\n", errstr);
688                 ret = false;
689         } else if (!W_ERROR_IS_OK(r.out.result)) {
690                 printf("DsBind failed - %s\n", win_errstr(r.out.result));
691                 ret = false;
692         }
693
694         return ret;
695 }
696
697 bool torture_rpc_drsuapi(struct torture_context *torture)
698 {
699         NTSTATUS status;
700         struct dcerpc_pipe *p;
701         bool ret = true;
702         struct DsPrivate priv;
703         struct cli_credentials *machine_credentials;
704
705         ZERO_STRUCT(priv);
706
707         priv.join = torture_join_domain(torture, TEST_MACHINE_NAME, ACB_SVRTRUST, 
708                                        &machine_credentials);
709         if (!priv.join) {
710                 torture_fail(torture, "Failed to join as BDC");
711         }
712
713         status = torture_rpc_connection(torture, 
714                                         &p, 
715                                         &ndr_table_drsuapi);
716         if (!NT_STATUS_IS_OK(status)) {
717                 torture_leave_domain(torture, priv.join);
718                 torture_fail(torture, "Unable to connect to DRSUAPI pipe");
719         }
720
721         ret &= test_DsBind(p, torture, &priv);
722 #if 0
723         ret &= test_QuerySitesByCost(p, torture, &priv);
724 #endif
725         ret &= test_DsGetDomainControllerInfo(p, torture, &priv);
726
727         ret &= test_DsCrackNames(torture, p, torture, &priv);
728
729         ret &= test_DsWriteAccountSpn(p, torture, &priv);
730
731         ret &= test_DsReplicaGetInfo(p, torture, &priv);
732
733         ret &= test_DsReplicaSync(p, torture, &priv);
734
735         ret &= test_DsReplicaUpdateRefs(p, torture, &priv);
736
737         ret &= test_DsGetNCChanges(p, torture, &priv);
738
739         ret &= test_DsUnbind(p, torture, &priv);
740
741         torture_leave_domain(torture, priv.join);
742
743         return ret;
744 }
745
746
747 bool torture_rpc_drsuapi_cracknames(struct torture_context *torture)
748 {
749         NTSTATUS status;
750         struct dcerpc_pipe *p;
751         bool ret = true;
752         struct DsPrivate priv;
753         struct cli_credentials *machine_credentials;
754
755         torture_comment(torture, "Connected to DRSUAPI pipe\n");
756
757         ZERO_STRUCT(priv);
758
759         priv.join = torture_join_domain(torture, TEST_MACHINE_NAME, ACB_SVRTRUST, 
760                                        &machine_credentials);
761         if (!priv.join) {
762                 torture_fail(torture, "Failed to join as BDC\n");
763         }
764
765         status = torture_rpc_connection(torture, 
766                                         &p, 
767                                         &ndr_table_drsuapi);
768         if (!NT_STATUS_IS_OK(status)) {
769                 torture_leave_domain(torture, priv.join);
770                 torture_fail(torture, "Unable to connect to DRSUAPI pipe");
771         }
772
773         ret &= test_DsBind(p, torture, &priv);
774
775         if (ret) {
776                 /* We don't care if this fails, we just need some info from it */
777                 test_DsGetDomainControllerInfo(p, torture, &priv);
778                 
779                 ret &= test_DsCrackNames(torture, p, torture, &priv);
780                 
781                 ret &= test_DsUnbind(p, torture, &priv);
782         }
783
784         torture_leave_domain(torture, priv.join);
785
786         return ret;
787 }
788