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