s4/drs: propagate DRS_ extension flags in code base
[ira/wip.git] / source4 / rpc_server / drsuapi / dcesrv_drsuapi.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    endpoint server for the drsuapi pipe
5
6    Copyright (C) Stefan Metzmacher 2004
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_drsuapi.h"
25 #include "rpc_server/dcerpc_server.h"
26 #include "rpc_server/common/common.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
29 #include "libcli/security/security.h"
30 #include "auth/auth.h"
31 #include "param/param.h"
32
33 #define DRSUAPI_UNSUPPORTED(fname) do { \
34         DEBUG(1,(__location__ ": Unsupported DRS call %s\n", #fname)); \
35         if (DEBUGLVL(2)) NDR_PRINT_IN_DEBUG(fname, r); \
36         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR); \
37 } while (0)
38
39 /* 
40   drsuapi_DsBind 
41 */
42 static WERROR dcesrv_drsuapi_DsBind(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
43                        struct drsuapi_DsBind *r)
44 {
45         struct drsuapi_bind_state *b_state;
46         struct dcesrv_handle *handle;
47         struct drsuapi_DsBindInfoCtr *bind_info;
48         struct GUID site_guid;
49         struct ldb_result *site_res;
50         struct ldb_dn *server_site_dn;
51         static const char *site_attrs[] = { "objectGUID", NULL };
52         struct ldb_result *ntds_res;
53         struct ldb_dn *ntds_dn;
54         static const char *ntds_attrs[] = { "ms-DS-ReplicationEpoch", NULL };
55         uint32_t pid;
56         uint32_t repl_epoch;
57         int ret;
58         struct auth_session_info *auth_info;
59         WERROR werr;
60
61         r->out.bind_info = NULL;
62         ZERO_STRUCTP(r->out.bind_handle);
63
64         b_state = talloc_zero(mem_ctx, struct drsuapi_bind_state);
65         W_ERROR_HAVE_NO_MEMORY(b_state);
66
67         /* if this is a DC connecting, give them system level access */
68         werr = drs_security_level_check(dce_call, NULL);
69         if (W_ERROR_IS_OK(werr)) {
70                 DEBUG(3,(__location__ ": doing DsBind with system_session\n"));
71                 auth_info = system_session(dce_call->conn->dce_ctx->lp_ctx);
72         } else {
73                 auth_info = dce_call->conn->auth_state.session_info;
74         }
75
76         /*
77          * connect to the samdb
78          */
79         b_state->sam_ctx = samdb_connect(b_state, dce_call->event_ctx, 
80                                          dce_call->conn->dce_ctx->lp_ctx, auth_info); 
81         if (!b_state->sam_ctx) {
82                 return WERR_FOOBAR;
83         }
84
85         /*
86          * find out the guid of our own site
87          */
88         server_site_dn = samdb_server_site_dn(b_state->sam_ctx, mem_ctx);
89         W_ERROR_HAVE_NO_MEMORY(server_site_dn);
90
91         ret = ldb_search(b_state->sam_ctx, mem_ctx, &site_res,
92                                  server_site_dn, LDB_SCOPE_BASE, site_attrs,
93                                  "(objectClass=*)");
94         if (ret != LDB_SUCCESS) {
95                 return WERR_DS_DRA_INTERNAL_ERROR;
96         }
97         if (site_res->count != 1) {
98                 return WERR_DS_DRA_INTERNAL_ERROR;
99         }
100         site_guid = samdb_result_guid(site_res->msgs[0], "objectGUID");
101
102         /*
103          * lookup the local servers Replication Epoch
104          */
105         ntds_dn = samdb_ntds_settings_dn(b_state->sam_ctx);
106         W_ERROR_HAVE_NO_MEMORY(ntds_dn);
107
108         ret = ldb_search(b_state->sam_ctx, mem_ctx, &ntds_res,
109                                  ntds_dn, LDB_SCOPE_BASE, ntds_attrs,
110                                  "(objectClass=*)");
111         if (ret != LDB_SUCCESS) {
112                 return WERR_DS_DRA_INTERNAL_ERROR;
113         }
114         if (ntds_res->count != 1) {
115                 return WERR_DS_DRA_INTERNAL_ERROR;
116         }
117         repl_epoch = samdb_result_uint(ntds_res->msgs[0], "ms-DS-ReplicationEpoch", 0);
118
119         /*
120          * The "process identifier" of the client.
121          * According to the WSPP docs, sectin 5.35, this is
122          * for informational and debugging purposes only.
123          * The assignment is implementation specific.
124          */
125         pid = 0;
126
127         /*
128          * store the clients bind_guid
129          */
130         if (r->in.bind_guid) {
131                 b_state->remote_bind_guid = *r->in.bind_guid;
132         }
133
134         /*
135          * store the clients bind_info
136          */
137         if (r->in.bind_info) {
138                 switch (r->in.bind_info->length) {
139                 case 24: {
140                         struct drsuapi_DsBindInfo24 *info24;
141                         info24 = &r->in.bind_info->info.info24;
142                         b_state->remote_info28.supported_extensions     = info24->supported_extensions;
143                         b_state->remote_info28.site_guid                = info24->site_guid;
144                         b_state->remote_info28.pid                      = info24->pid;
145                         b_state->remote_info28.repl_epoch               = 0;
146                         break;
147                 }
148                 case 28:
149                         b_state->remote_info28 = r->in.bind_info->info.info28;
150                         break;
151                 }
152         }
153
154         /*
155          * fill in our local bind info 28
156          */
157         b_state->local_info28.supported_extensions      = 0;
158         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
159         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
160         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
161         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
162 #if 0 /* we don't support MSZIP compression (only decompression) */
163         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
164 #endif
165         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
166         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
167         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
168         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
169         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
170         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
171         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
172         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
173         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
174         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
175         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
176         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
177         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
178         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
179         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5;
180         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
181         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
182         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
183         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
184         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
185         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
186         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
187         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
188         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
189 #if 0 /* we don't support XPRESS compression yet */
190         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
191 #endif
192         b_state->local_info28.site_guid                 = site_guid;
193         b_state->local_info28.pid                       = pid;
194         b_state->local_info28.repl_epoch                = repl_epoch;
195
196         /*
197          * allocate the return bind_info
198          */
199         bind_info = talloc(mem_ctx, struct drsuapi_DsBindInfoCtr);
200         W_ERROR_HAVE_NO_MEMORY(bind_info);
201
202         bind_info->length       = 28;
203         bind_info->info.info28  = b_state->local_info28;
204
205         /*
206          * allocate a bind handle
207          */
208         handle = dcesrv_handle_new(dce_call->context, DRSUAPI_BIND_HANDLE);
209         W_ERROR_HAVE_NO_MEMORY(handle);
210         handle->data = talloc_steal(handle, b_state);
211
212         /*
213          * prepare reply
214          */
215         r->out.bind_info = bind_info;
216         *r->out.bind_handle = handle->wire_handle;
217
218         return WERR_OK;
219 }
220
221
222 /* 
223   drsuapi_DsUnbind 
224 */
225 static WERROR dcesrv_drsuapi_DsUnbind(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
226                                struct drsuapi_DsUnbind *r)
227 {
228         struct dcesrv_handle *h;
229
230         *r->out.bind_handle = *r->in.bind_handle;
231
232         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
233
234         talloc_free(h);
235
236         ZERO_STRUCTP(r->out.bind_handle);
237
238         return WERR_OK;
239 }
240
241
242 /* 
243   drsuapi_DsReplicaSync 
244 */
245 static WERROR dcesrv_drsuapi_DsReplicaSync(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
246                                            struct drsuapi_DsReplicaSync *r)
247 {
248         WERROR status;
249
250         status = drs_security_level_check(dce_call, "DsReplicaSync");
251         if (!W_ERROR_IS_OK(status)) {
252                 return status;
253         }
254
255         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSREPLICASYNC,
256                                      &ndr_table_drsuapi,
257                                      "dreplsrv", "DsReplicaSync");
258
259         return WERR_OK;
260 }
261
262
263 /* 
264   drsuapi_DsReplicaAdd 
265 */
266 static WERROR dcesrv_drsuapi_DsReplicaAdd(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
267                                           struct drsuapi_DsReplicaAdd *r)
268 {
269         DRSUAPI_UNSUPPORTED(drsuapi_DsReplicaAdd);
270 }
271
272
273 /* 
274   drsuapi_DsReplicaDel 
275 */
276 static WERROR dcesrv_drsuapi_DsReplicaDel(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
277                                           struct drsuapi_DsReplicaDel *r)
278 {
279         DRSUAPI_UNSUPPORTED(drsuapi_DsReplicaDel);
280 }
281
282
283 /* 
284   drsuapi_DsReplicaModify 
285 */
286 static WERROR dcesrv_drsuapi_DsReplicaMod(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
287                                           struct drsuapi_DsReplicaMod *r)
288 {
289         DRSUAPI_UNSUPPORTED(drsuapi_DsReplicaMod);
290 }
291
292
293 /* 
294   DRSUAPI_VERIFY_NAMES 
295 */
296 static WERROR dcesrv_DRSUAPI_VERIFY_NAMES(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
297                        struct DRSUAPI_VERIFY_NAMES *r)
298 {
299         DRSUAPI_UNSUPPORTED(DRSUAPI_VERIFY_NAMES);
300 }
301
302
303 /* 
304   drsuapi_DsGetMemberships 
305 */
306 static WERROR dcesrv_drsuapi_DsGetMemberships(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
307                        struct drsuapi_DsGetMemberships *r)
308 {
309         DRSUAPI_UNSUPPORTED(drsuapi_DsGetMemberships);
310 }
311
312
313 /* 
314   DRSUAPI_INTER_DOMAIN_MOVE 
315 */
316 static WERROR dcesrv_DRSUAPI_INTER_DOMAIN_MOVE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
317                        struct DRSUAPI_INTER_DOMAIN_MOVE *r)
318 {
319         DRSUAPI_UNSUPPORTED(DRSUAPI_INTER_DOMAIN_MOVE);
320 }
321
322
323 /* 
324   drsuapi_DsGetNT4ChangeLog 
325 */
326 static WERROR dcesrv_drsuapi_DsGetNT4ChangeLog(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
327                        struct drsuapi_DsGetNT4ChangeLog *r)
328 {
329         DRSUAPI_UNSUPPORTED(drsuapi_DsGetNT4ChangeLog);
330 }
331
332
333 /* 
334   drsuapi_DsCrackNames 
335 */
336 static WERROR dcesrv_drsuapi_DsCrackNames(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
337                             struct drsuapi_DsCrackNames *r)
338 {
339         WERROR status;
340         struct drsuapi_bind_state *b_state;
341         struct dcesrv_handle *h;
342
343         *r->out.level_out = r->in.level;
344
345         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
346         b_state = h->data;
347
348         r->out.ctr = talloc_zero(mem_ctx, union drsuapi_DsNameCtr);
349         W_ERROR_HAVE_NO_MEMORY(r->out.ctr);
350
351         switch (r->in.level) {
352                 case 1: {
353                         struct drsuapi_DsNameCtr1 *ctr1;
354                         struct drsuapi_DsNameInfo1 *names;
355                         int count;
356                         int i;
357
358                         ctr1 = talloc(mem_ctx, struct drsuapi_DsNameCtr1);
359                         W_ERROR_HAVE_NO_MEMORY(ctr1);
360
361                         count = r->in.req->req1.count;
362                         names = talloc_array(mem_ctx, struct drsuapi_DsNameInfo1, count);
363                         W_ERROR_HAVE_NO_MEMORY(names);
364
365                         for (i=0; i < count; i++) {
366                                 status = DsCrackNameOneName(b_state->sam_ctx, mem_ctx,
367                                                             r->in.req->req1.format_flags,
368                                                             r->in.req->req1.format_offered,
369                                                             r->in.req->req1.format_desired,
370                                                             r->in.req->req1.names[i].str,
371                                                             &names[i]);
372                                 if (!W_ERROR_IS_OK(status)) {
373                                         return status;
374                                 }
375                         }
376
377                         ctr1->count = count;
378                         ctr1->array = names;
379                         r->out.ctr->ctr1 = ctr1;
380
381                         return WERR_OK;
382                 }
383         }
384         
385         return WERR_UNKNOWN_LEVEL;
386 }
387
388
389 /* 
390   drsuapi_DsRemoveDSServer
391 */
392 static WERROR dcesrv_drsuapi_DsRemoveDSServer(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
393                                        struct drsuapi_DsRemoveDSServer *r)
394 {
395         struct drsuapi_bind_state *b_state;
396         struct dcesrv_handle *h;
397         struct ldb_dn *ntds_dn;
398         int ret;
399         bool ok;
400         WERROR status;
401
402         ZERO_STRUCT(r->out.res);
403         *r->out.level_out = 1;
404
405         status = drs_security_level_check(dce_call, "DsRemoveDSServer");
406         if (!W_ERROR_IS_OK(status)) {
407                 return status;
408         }
409
410         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
411         b_state = h->data;
412
413         switch (r->in.level) {
414         case 1:
415                 ntds_dn = ldb_dn_new(mem_ctx, b_state->sam_ctx, r->in.req->req1.server_dn);
416                 W_ERROR_HAVE_NO_MEMORY(ntds_dn);
417
418                 ok = ldb_dn_validate(ntds_dn);
419                 if (!ok) {
420                         return WERR_FOOBAR;
421                 }
422
423                 /* TODO: it's likely that we need more checks here */
424
425                 ok = ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings");
426                 if (!ok) {
427                         return WERR_FOOBAR;
428                 }
429
430                 if (r->in.req->req1.commit) {
431                         ret = ldb_delete(b_state->sam_ctx, ntds_dn);
432                         if (ret != LDB_SUCCESS) {
433                                 return WERR_FOOBAR;
434                         }
435                 }
436
437                 return WERR_OK;
438         default:
439                 break;
440         }
441
442         return WERR_FOOBAR;
443 }
444
445
446 /* 
447   DRSUAPI_REMOVE_DS_DOMAIN 
448 */
449 static WERROR dcesrv_DRSUAPI_REMOVE_DS_DOMAIN(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
450                        struct DRSUAPI_REMOVE_DS_DOMAIN *r)
451 {
452         DRSUAPI_UNSUPPORTED(DRSUAPI_REMOVE_DS_DOMAIN);
453 }
454
455 /* Obtain the site name from a server DN */
456 static const char *result_site_name(struct ldb_dn *site_dn)
457 {
458         /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
459         const struct ldb_val *val = ldb_dn_get_component_val(site_dn, 2);
460         const char *name = ldb_dn_get_component_name(site_dn, 2);
461
462         if (!name || (ldb_attr_cmp(name, "cn") != 0)) {
463                 /* Ensure this matches the format.  This gives us a
464                  * bit more confidence that a 'cn' value will be a
465                  * ascii string */
466                 return NULL;
467         }
468         if (val) {
469                 return (char *)val->data;
470         }
471         return NULL;
472 }
473
474 /* 
475   drsuapi_DsGetDomainControllerInfo 
476 */
477 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo_1(struct drsuapi_bind_state *b_state, 
478                                                 TALLOC_CTX *mem_ctx,
479                                                 struct drsuapi_DsGetDomainControllerInfo *r)
480 {
481         struct ldb_dn *sites_dn;
482         struct ldb_result *res;
483
484         const char *attrs_account_1[] = { "cn", "dnsHostName", NULL };
485         const char *attrs_account_2[] = { "cn", "dnsHostName", "objectGUID", NULL };
486
487         const char *attrs_none[] = { NULL };
488
489         const char *attrs_site[] = { "objectGUID", NULL };
490
491         const char *attrs_ntds[] = { "options", "objectGUID", NULL };
492
493         const char *attrs_1[] = { "serverReference", "cn", "dnsHostName", NULL };
494         const char *attrs_2[] = { "serverReference", "cn", "dnsHostName", "objectGUID", NULL };
495         const char **attrs;
496
497         struct drsuapi_DsGetDCInfoCtr1 *ctr1;
498         struct drsuapi_DsGetDCInfoCtr2 *ctr2;
499
500         int ret, i;
501
502         *r->out.level_out = r->in.req->req1.level;
503         r->out.ctr = talloc(mem_ctx, union drsuapi_DsGetDCInfoCtr);
504         W_ERROR_HAVE_NO_MEMORY(r->out.ctr);
505
506         sites_dn = samdb_sites_dn(b_state->sam_ctx, mem_ctx);
507         if (!sites_dn) {
508                 return WERR_DS_OBJ_NOT_FOUND;
509         }
510
511         switch (*r->out.level_out) {
512         case -1:
513                 /* this level is not like the others */
514                 return WERR_UNKNOWN_LEVEL;
515         case 1:
516                 attrs = attrs_1;
517                 break;
518         case 2:
519                 attrs = attrs_2;
520                 break;
521         default:
522                 return WERR_UNKNOWN_LEVEL;
523         }
524
525         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res, sites_dn, LDB_SCOPE_SUBTREE, attrs,
526                                  "objectClass=server");
527         
528         if (ret) {
529                 DEBUG(1, ("searching for servers in sites DN %s failed: %s\n", 
530                           ldb_dn_get_linearized(sites_dn), ldb_errstring(b_state->sam_ctx)));
531                 return WERR_GENERAL_FAILURE;
532         }
533
534         switch (*r->out.level_out) {
535         case 1:
536                 ctr1 = &r->out.ctr->ctr1;
537                 ctr1->count = res->count;
538                 ctr1->array = talloc_zero_array(mem_ctx, 
539                                                 struct drsuapi_DsGetDCInfo1, 
540                                                 res->count);
541                 for (i=0; i < res->count; i++) {
542                         struct ldb_dn *domain_dn;
543                         struct ldb_result *res_domain;
544                         struct ldb_result *res_account;
545                         struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
546                         
547                         struct ldb_dn *ref_dn
548                                 = ldb_msg_find_attr_as_dn(b_state->sam_ctx, 
549                                                           mem_ctx, res->msgs[i], 
550                                                           "serverReference");
551
552                         if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
553                                 return WERR_NOMEM;
554                         }
555
556                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
557                                                  LDB_SCOPE_BASE, attrs_account_1, "objectClass=computer");
558                         if (ret == LDB_SUCCESS && res_account->count == 1) {
559                                 const char *errstr;
560                                 ctr1->array[i].dns_name
561                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
562                                 ctr1->array[i].netbios_name
563                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
564                                 ctr1->array[i].computer_dn
565                                         = ldb_dn_get_linearized(res_account->msgs[0]->dn);
566
567                                 /* Determine if this is the PDC */
568                                 ret = samdb_search_for_parent_domain(b_state->sam_ctx, 
569                                                                      mem_ctx, res_account->msgs[0]->dn,
570                                                                      &domain_dn, &errstr);
571                                 
572                                 if (ret == LDB_SUCCESS) {
573                                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
574                                                                  LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
575                                                                  ldb_dn_get_linearized(ntds_dn));
576                                         if (ret) {
577                                                 return WERR_GENERAL_FAILURE;
578                                         }
579                                         if (res_domain->count == 1) {
580                                                 ctr1->array[i].is_pdc = true;
581                                         }
582                                 }
583                         }
584                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
585                                 DEBUG(5, ("warning: searching for computer DN %s failed: %s\n", 
586                                           ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
587                         }
588
589                         /* Look at server DN and extract site component */
590                         ctr1->array[i].site_name = result_site_name(res->msgs[i]->dn);
591                         ctr1->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
592
593
594                         ctr1->array[i].is_enabled = true;
595
596                 }
597                 break;
598         case 2:
599                 ctr2 = &r->out.ctr->ctr2;
600                 ctr2->count = res->count;
601                 ctr2->array = talloc_zero_array(mem_ctx, 
602                                                  struct drsuapi_DsGetDCInfo2, 
603                                                  res->count);
604                 for (i=0; i < res->count; i++) {
605                         struct ldb_dn *domain_dn;
606                         struct ldb_result *res_domain;
607                         struct ldb_result *res_account;
608                         struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
609                         struct ldb_result *res_ntds;
610                         struct ldb_dn *site_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
611                         struct ldb_result *res_site;
612                         struct ldb_dn *ref_dn
613                                 = ldb_msg_find_attr_as_dn(b_state->sam_ctx, 
614                                                           mem_ctx, res->msgs[i], 
615                                                           "serverReference");
616
617                         if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
618                                 return WERR_NOMEM;
619                         }
620
621                         /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
622                         if (!site_dn || !ldb_dn_remove_child_components(site_dn, 2)) {
623                                 return WERR_NOMEM;
624                         }
625
626                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_ntds, ntds_dn,
627                                                  LDB_SCOPE_BASE, attrs_ntds, "objectClass=nTDSDSA");
628                         if (ret == LDB_SUCCESS && res_ntds->count == 1) {
629                                 ctr2->array[i].is_gc
630                                         = (ldb_msg_find_attr_as_int(res_ntds->msgs[0], "options", 0) == 1);
631                                 ctr2->array[i].ntds_guid 
632                                         = samdb_result_guid(res_ntds->msgs[0], "objectGUID");
633                                 ctr2->array[i].ntds_dn = ldb_dn_get_linearized(res_ntds->msgs[0]->dn);
634                         }
635                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
636                                 DEBUG(5, ("warning: searching for NTDS DN %s failed: %s\n", 
637                                           ldb_dn_get_linearized(ntds_dn), ldb_errstring(b_state->sam_ctx)));
638                         }
639
640                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_site, site_dn,
641                                                  LDB_SCOPE_BASE, attrs_site, "objectClass=site");
642                         if (ret == LDB_SUCCESS && res_site->count == 1) {
643                                 ctr2->array[i].site_guid 
644                                         = samdb_result_guid(res_site->msgs[0], "objectGUID");
645                                 ctr2->array[i].site_dn = ldb_dn_get_linearized(res_site->msgs[0]->dn);
646                         }
647                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
648                                 DEBUG(5, ("warning: searching for site DN %s failed: %s\n", 
649                                           ldb_dn_get_linearized(site_dn), ldb_errstring(b_state->sam_ctx)));
650                         }
651
652                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
653                                                  LDB_SCOPE_BASE, attrs_account_2, "objectClass=computer");
654                         if (ret == LDB_SUCCESS && res_account->count == 1) {
655                                 const char *errstr;
656                                 ctr2->array[i].dns_name
657                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
658                                 ctr2->array[i].netbios_name
659                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
660                                 ctr2->array[i].computer_dn = ldb_dn_get_linearized(res_account->msgs[0]->dn);
661                                 ctr2->array[i].computer_guid 
662                                         = samdb_result_guid(res_account->msgs[0], "objectGUID");
663
664                                 /* Determine if this is the PDC */
665                                 ret = samdb_search_for_parent_domain(b_state->sam_ctx, 
666                                                                      mem_ctx, res_account->msgs[0]->dn,
667                                                                      &domain_dn, &errstr);
668                                 
669                                 if (ret == LDB_SUCCESS) {
670                                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
671                                                                  LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
672                                                                  ldb_dn_get_linearized(ntds_dn));
673                                         if (ret == LDB_SUCCESS && res_domain->count == 1) {
674                                                 ctr2->array[i].is_pdc = true;
675                                         }
676                                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
677                                                 DEBUG(5, ("warning: searching for domain DN %s failed: %s\n", 
678                                                           ldb_dn_get_linearized(domain_dn), ldb_errstring(b_state->sam_ctx)));
679                                         }
680                                 }
681                         }
682                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
683                                 DEBUG(5, ("warning: searching for computer account DN %s failed: %s\n", 
684                                           ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
685                         }
686
687                         /* Look at server DN and extract site component */
688                         ctr2->array[i].site_name = result_site_name(res->msgs[i]->dn);
689                         ctr2->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
690                         ctr2->array[i].server_guid 
691                                 = samdb_result_guid(res->msgs[i], "objectGUID");
692
693                         ctr2->array[i].is_enabled = true;
694
695                 }
696                 break;
697         }
698         return WERR_OK;
699 }
700
701 /* 
702   drsuapi_DsGetDomainControllerInfo 
703 */
704 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
705                                                 struct drsuapi_DsGetDomainControllerInfo *r)
706 {
707         struct dcesrv_handle *h;
708         struct drsuapi_bind_state *b_state;     
709         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
710         b_state = h->data;
711
712         switch (r->in.level) {
713         case 1:
714                 return dcesrv_drsuapi_DsGetDomainControllerInfo_1(b_state, mem_ctx, r);
715         }
716
717         return WERR_UNKNOWN_LEVEL;
718 }
719
720
721
722 /* 
723   drsuapi_DsExecuteKCC 
724 */
725 static WERROR dcesrv_drsuapi_DsExecuteKCC(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
726                                   struct drsuapi_DsExecuteKCC *r)
727 {
728         WERROR status;
729         status = drs_security_level_check(dce_call, "DsExecuteKCC");
730
731         if (!W_ERROR_IS_OK(status)) {
732                 return status;
733         }
734
735         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSEXECUTEKCC,
736                                                                 &ndr_table_drsuapi, "kccsrv", "DsExecuteKCC");
737         return WERR_OK;
738 }
739
740
741 /* 
742   drsuapi_DsReplicaGetInfo 
743 */
744 static WERROR dcesrv_drsuapi_DsReplicaGetInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
745                        struct drsuapi_DsReplicaGetInfo *r)
746 {
747         enum security_user_level level;
748
749         if (!lp_parm_bool(dce_call->conn->dce_ctx->lp_ctx, NULL,
750                          "drs", "disable_sec_check", false)) {
751                 level = security_session_user_level(dce_call->conn->auth_state.session_info);
752                 if (level < SECURITY_ADMINISTRATOR) {
753                         DEBUG(1,(__location__ ": Administrator access required for DsReplicaGetInfo\n"));
754                         security_token_debug(2, dce_call->conn->auth_state.session_info->security_token);
755                         return WERR_DS_DRA_ACCESS_DENIED;
756                 }
757         }
758
759         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSREPLICAGETINFO,
760                                      &ndr_table_drsuapi, "kccsrv", "DsReplicaGetInfo");
761
762         return WERR_OK;
763 }
764
765
766 /* 
767   DRSUAPI_ADD_SID_HISTORY 
768 */
769 static WERROR dcesrv_DRSUAPI_ADD_SID_HISTORY(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
770                        struct DRSUAPI_ADD_SID_HISTORY *r)
771 {
772         DRSUAPI_UNSUPPORTED(DRSUAPI_ADD_SID_HISTORY);
773 }
774
775 /* 
776   drsuapi_DsGetMemberships2 
777 */
778 static WERROR dcesrv_drsuapi_DsGetMemberships2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
779                        struct drsuapi_DsGetMemberships2 *r)
780 {
781         DRSUAPI_UNSUPPORTED(drsuapi_DsGetMemberships2);
782 }
783
784 /* 
785   DRSUAPI_REPLICA_VERIFY_OBJECTS 
786 */
787 static WERROR dcesrv_DRSUAPI_REPLICA_VERIFY_OBJECTS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
788                        struct DRSUAPI_REPLICA_VERIFY_OBJECTS *r)
789 {
790         DRSUAPI_UNSUPPORTED(DRSUAPI_REPLICA_VERIFY_OBJECTS);
791 }
792
793
794 /* 
795   DRSUAPI_GET_OBJECT_EXISTENCE 
796 */
797 static WERROR dcesrv_DRSUAPI_GET_OBJECT_EXISTENCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
798                        struct DRSUAPI_GET_OBJECT_EXISTENCE *r)
799 {
800         DRSUAPI_UNSUPPORTED(DRSUAPI_GET_OBJECT_EXISTENCE);
801 }
802
803
804 /* 
805   drsuapi_QuerySitesByCost 
806 */
807 static WERROR dcesrv_drsuapi_QuerySitesByCost(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
808                        struct drsuapi_QuerySitesByCost *r)
809 {
810         DRSUAPI_UNSUPPORTED(drsuapi_QuerySitesByCost);
811 }
812
813
814 /* include the generated boilerplate */
815 #include "librpc/gen_ndr/ndr_drsuapi_s.c"