s4-drs: added domain_sid to DRS security checks
[nivanova/samba-autobuild/.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, SECURITY_DOMAIN_CONTROLLER, 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", SECURITY_DOMAIN_CONTROLLER, NULL);
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                         uint32_t i, count;
356
357                         ctr1 = talloc(mem_ctx, struct drsuapi_DsNameCtr1);
358                         W_ERROR_HAVE_NO_MEMORY(ctr1);
359
360                         count = r->in.req->req1.count;
361                         names = talloc_array(mem_ctx, struct drsuapi_DsNameInfo1, count);
362                         W_ERROR_HAVE_NO_MEMORY(names);
363
364                         for (i=0; i < count; i++) {
365                                 status = DsCrackNameOneName(b_state->sam_ctx, mem_ctx,
366                                                             r->in.req->req1.format_flags,
367                                                             r->in.req->req1.format_offered,
368                                                             r->in.req->req1.format_desired,
369                                                             r->in.req->req1.names[i].str,
370                                                             &names[i]);
371                                 if (!W_ERROR_IS_OK(status)) {
372                                         return status;
373                                 }
374                         }
375
376                         ctr1->count = count;
377                         ctr1->array = names;
378                         r->out.ctr->ctr1 = ctr1;
379
380                         return WERR_OK;
381                 }
382         }
383         
384         return WERR_UNKNOWN_LEVEL;
385 }
386
387
388 /* 
389   drsuapi_DsRemoveDSServer
390 */
391 static WERROR dcesrv_drsuapi_DsRemoveDSServer(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
392                                        struct drsuapi_DsRemoveDSServer *r)
393 {
394         struct drsuapi_bind_state *b_state;
395         struct dcesrv_handle *h;
396         struct ldb_dn *ntds_dn;
397         int ret;
398         bool ok;
399         WERROR status;
400
401         *r->out.level_out = 1;
402
403         status = drs_security_level_check(dce_call, "DsRemoveDSServer", SECURITY_DOMAIN_CONTROLLER, NULL);
404         if (!W_ERROR_IS_OK(status)) {
405                 return status;
406         }
407
408         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
409         b_state = h->data;
410
411         switch (r->in.level) {
412         case 1:
413                 ntds_dn = ldb_dn_new(mem_ctx, b_state->sam_ctx, r->in.req->req1.server_dn);
414                 W_ERROR_HAVE_NO_MEMORY(ntds_dn);
415
416                 ok = ldb_dn_validate(ntds_dn);
417                 if (!ok) {
418                         return WERR_FOOBAR;
419                 }
420
421                 /* TODO: it's likely that we need more checks here */
422
423                 ok = ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings");
424                 if (!ok) {
425                         return WERR_FOOBAR;
426                 }
427
428                 if (r->in.req->req1.commit) {
429                         ret = ldb_delete(b_state->sam_ctx, ntds_dn);
430                         if (ret != LDB_SUCCESS) {
431                                 return WERR_FOOBAR;
432                         }
433                 }
434
435                 return WERR_OK;
436         default:
437                 break;
438         }
439
440         return WERR_FOOBAR;
441 }
442
443
444 /* 
445   DRSUAPI_REMOVE_DS_DOMAIN 
446 */
447 static WERROR dcesrv_DRSUAPI_REMOVE_DS_DOMAIN(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
448                        struct DRSUAPI_REMOVE_DS_DOMAIN *r)
449 {
450         DRSUAPI_UNSUPPORTED(DRSUAPI_REMOVE_DS_DOMAIN);
451 }
452
453 /* Obtain the site name from a server DN */
454 static const char *result_site_name(struct ldb_dn *server_dn)
455 {
456         /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
457         const struct ldb_val *val = ldb_dn_get_component_val(server_dn, 2);
458         const char *name = ldb_dn_get_component_name(server_dn, 2);
459
460         if (!name || (ldb_attr_cmp(name, "cn") != 0)) {
461                 /* Ensure this matches the format.  This gives us a
462                  * bit more confidence that a 'cn' value will be a
463                  * ascii string */
464                 return NULL;
465         }
466         if (val) {
467                 return (char *)val->data;
468         }
469         return NULL;
470 }
471
472 /* 
473   drsuapi_DsGetDomainControllerInfo 
474 */
475 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo_1(struct drsuapi_bind_state *b_state, 
476                                                 TALLOC_CTX *mem_ctx,
477                                                 struct drsuapi_DsGetDomainControllerInfo *r)
478 {
479         struct ldb_dn *sites_dn;
480         struct ldb_result *res;
481
482         const char *attrs_account_1[] = { "cn", "dnsHostName", NULL };
483         const char *attrs_account_2[] = { "cn", "dnsHostName", "objectGUID", NULL };
484
485         const char *attrs_none[] = { NULL };
486
487         const char *attrs_site[] = { "objectGUID", NULL };
488
489         const char *attrs_ntds[] = { "options", "objectGUID", NULL };
490
491         const char *attrs_1[] = { "serverReference", "cn", "dnsHostName", NULL };
492         const char *attrs_2[] = { "serverReference", "cn", "dnsHostName", "objectGUID", NULL };
493         const char **attrs;
494
495         struct drsuapi_DsGetDCInfoCtr1 *ctr1;
496         struct drsuapi_DsGetDCInfoCtr2 *ctr2;
497
498         int ret;
499         unsigned int i;
500
501         *r->out.level_out = r->in.req->req1.level;
502         r->out.ctr = talloc(mem_ctx, union drsuapi_DsGetDCInfoCtr);
503         W_ERROR_HAVE_NO_MEMORY(r->out.ctr);
504
505         sites_dn = samdb_sites_dn(b_state->sam_ctx, mem_ctx);
506         if (!sites_dn) {
507                 return WERR_DS_OBJ_NOT_FOUND;
508         }
509
510         switch (*r->out.level_out) {
511         case -1:
512                 /* this level is not like the others */
513                 return WERR_UNKNOWN_LEVEL;
514         case 1:
515                 attrs = attrs_1;
516                 break;
517         case 2:
518                 attrs = attrs_2;
519                 break;
520         default:
521                 return WERR_UNKNOWN_LEVEL;
522         }
523
524         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res, sites_dn, LDB_SCOPE_SUBTREE, attrs,
525                                  "objectClass=server");
526         
527         if (ret) {
528                 DEBUG(1, ("searching for servers in sites DN %s failed: %s\n", 
529                           ldb_dn_get_linearized(sites_dn), ldb_errstring(b_state->sam_ctx)));
530                 return WERR_GENERAL_FAILURE;
531         }
532
533         switch (*r->out.level_out) {
534         case 1:
535                 ctr1 = &r->out.ctr->ctr1;
536                 ctr1->count = res->count;
537                 ctr1->array = talloc_zero_array(mem_ctx, 
538                                                 struct drsuapi_DsGetDCInfo1, 
539                                                 res->count);
540                 for (i=0; i < res->count; i++) {
541                         struct ldb_dn *domain_dn;
542                         struct ldb_result *res_domain;
543                         struct ldb_result *res_account;
544                         struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
545                         
546                         struct ldb_dn *ref_dn
547                                 = ldb_msg_find_attr_as_dn(b_state->sam_ctx, 
548                                                           mem_ctx, res->msgs[i], 
549                                                           "serverReference");
550
551                         if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
552                                 return WERR_NOMEM;
553                         }
554
555                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
556                                                  LDB_SCOPE_BASE, attrs_account_1, "objectClass=computer");
557                         if (ret == LDB_SUCCESS && res_account->count == 1) {
558                                 const char *errstr;
559                                 ctr1->array[i].dns_name
560                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
561                                 ctr1->array[i].netbios_name
562                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
563                                 ctr1->array[i].computer_dn
564                                         = ldb_dn_get_linearized(res_account->msgs[0]->dn);
565
566                                 /* Determine if this is the PDC */
567                                 ret = samdb_search_for_parent_domain(b_state->sam_ctx, 
568                                                                      mem_ctx, res_account->msgs[0]->dn,
569                                                                      &domain_dn, &errstr);
570                                 
571                                 if (ret == LDB_SUCCESS) {
572                                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
573                                                                  LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
574                                                                  ldb_dn_get_linearized(ntds_dn));
575                                         if (ret) {
576                                                 return WERR_GENERAL_FAILURE;
577                                         }
578                                         if (res_domain->count == 1) {
579                                                 ctr1->array[i].is_pdc = true;
580                                         }
581                                 }
582                         }
583                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
584                                 DEBUG(5, ("warning: searching for computer DN %s failed: %s\n", 
585                                           ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
586                         }
587
588                         /* Look at server DN and extract site component */
589                         ctr1->array[i].site_name = result_site_name(res->msgs[i]->dn);
590                         ctr1->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
591
592
593                         ctr1->array[i].is_enabled = true;
594
595                 }
596                 break;
597         case 2:
598                 ctr2 = &r->out.ctr->ctr2;
599                 ctr2->count = res->count;
600                 ctr2->array = talloc_zero_array(mem_ctx, 
601                                                  struct drsuapi_DsGetDCInfo2, 
602                                                  res->count);
603                 for (i=0; i < res->count; i++) {
604                         struct ldb_dn *domain_dn;
605                         struct ldb_result *res_domain;
606                         struct ldb_result *res_account;
607                         struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
608                         struct ldb_result *res_ntds;
609                         struct ldb_dn *site_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
610                         struct ldb_result *res_site;
611                         struct ldb_dn *ref_dn
612                                 = ldb_msg_find_attr_as_dn(b_state->sam_ctx, 
613                                                           mem_ctx, res->msgs[i], 
614                                                           "serverReference");
615
616                         if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
617                                 return WERR_NOMEM;
618                         }
619
620                         /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
621                         if (!site_dn || !ldb_dn_remove_child_components(site_dn, 2)) {
622                                 return WERR_NOMEM;
623                         }
624
625                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_ntds, ntds_dn,
626                                                  LDB_SCOPE_BASE, attrs_ntds, "objectClass=nTDSDSA");
627                         if (ret == LDB_SUCCESS && res_ntds->count == 1) {
628                                 ctr2->array[i].is_gc
629                                         = (ldb_msg_find_attr_as_int(res_ntds->msgs[0], "options", 0) == 1);
630                                 ctr2->array[i].ntds_guid 
631                                         = samdb_result_guid(res_ntds->msgs[0], "objectGUID");
632                                 ctr2->array[i].ntds_dn = ldb_dn_get_linearized(res_ntds->msgs[0]->dn);
633                         }
634                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
635                                 DEBUG(5, ("warning: searching for NTDS DN %s failed: %s\n", 
636                                           ldb_dn_get_linearized(ntds_dn), ldb_errstring(b_state->sam_ctx)));
637                         }
638
639                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_site, site_dn,
640                                                  LDB_SCOPE_BASE, attrs_site, "objectClass=site");
641                         if (ret == LDB_SUCCESS && res_site->count == 1) {
642                                 ctr2->array[i].site_guid 
643                                         = samdb_result_guid(res_site->msgs[0], "objectGUID");
644                                 ctr2->array[i].site_dn = ldb_dn_get_linearized(res_site->msgs[0]->dn);
645                         }
646                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
647                                 DEBUG(5, ("warning: searching for site DN %s failed: %s\n", 
648                                           ldb_dn_get_linearized(site_dn), ldb_errstring(b_state->sam_ctx)));
649                         }
650
651                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
652                                                  LDB_SCOPE_BASE, attrs_account_2, "objectClass=computer");
653                         if (ret == LDB_SUCCESS && res_account->count == 1) {
654                                 const char *errstr;
655                                 ctr2->array[i].dns_name
656                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
657                                 ctr2->array[i].netbios_name
658                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
659                                 ctr2->array[i].computer_dn = ldb_dn_get_linearized(res_account->msgs[0]->dn);
660                                 ctr2->array[i].computer_guid 
661                                         = samdb_result_guid(res_account->msgs[0], "objectGUID");
662
663                                 /* Determine if this is the PDC */
664                                 ret = samdb_search_for_parent_domain(b_state->sam_ctx, 
665                                                                      mem_ctx, res_account->msgs[0]->dn,
666                                                                      &domain_dn, &errstr);
667                                 
668                                 if (ret == LDB_SUCCESS) {
669                                         ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
670                                                                  LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
671                                                                  ldb_dn_get_linearized(ntds_dn));
672                                         if (ret == LDB_SUCCESS && res_domain->count == 1) {
673                                                 ctr2->array[i].is_pdc = true;
674                                         }
675                                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
676                                                 DEBUG(5, ("warning: searching for domain DN %s failed: %s\n", 
677                                                           ldb_dn_get_linearized(domain_dn), ldb_errstring(b_state->sam_ctx)));
678                                         }
679                                 }
680                         }
681                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
682                                 DEBUG(5, ("warning: searching for computer account DN %s failed: %s\n", 
683                                           ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
684                         }
685
686                         /* Look at server DN and extract site component */
687                         ctr2->array[i].site_name = result_site_name(res->msgs[i]->dn);
688                         ctr2->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
689                         ctr2->array[i].server_guid 
690                                 = samdb_result_guid(res->msgs[i], "objectGUID");
691
692                         ctr2->array[i].is_enabled = true;
693
694                 }
695                 break;
696         }
697         return WERR_OK;
698 }
699
700 /* 
701   drsuapi_DsGetDomainControllerInfo 
702 */
703 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
704                                                 struct drsuapi_DsGetDomainControllerInfo *r)
705 {
706         struct dcesrv_handle *h;
707         struct drsuapi_bind_state *b_state;     
708         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
709         b_state = h->data;
710
711         switch (r->in.level) {
712         case 1:
713                 return dcesrv_drsuapi_DsGetDomainControllerInfo_1(b_state, mem_ctx, r);
714         }
715
716         return WERR_UNKNOWN_LEVEL;
717 }
718
719
720
721 /* 
722   drsuapi_DsExecuteKCC 
723 */
724 static WERROR dcesrv_drsuapi_DsExecuteKCC(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
725                                   struct drsuapi_DsExecuteKCC *r)
726 {
727         WERROR status;
728         status = drs_security_level_check(dce_call, "DsExecuteKCC", SECURITY_DOMAIN_CONTROLLER, NULL);
729
730         if (!W_ERROR_IS_OK(status)) {
731                 return status;
732         }
733
734         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSEXECUTEKCC,
735                                                                 &ndr_table_drsuapi, "kccsrv", "DsExecuteKCC");
736         return WERR_OK;
737 }
738
739
740 /* 
741   drsuapi_DsReplicaGetInfo 
742 */
743 static WERROR dcesrv_drsuapi_DsReplicaGetInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
744                        struct drsuapi_DsReplicaGetInfo *r)
745 {
746         enum security_user_level level;
747
748         if (!lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx, NULL,
749                          "drs", "disable_sec_check", false)) {
750                 level = security_session_user_level(dce_call->conn->auth_state.session_info, NULL);
751                 if (level < SECURITY_ADMINISTRATOR) {
752                         DEBUG(1,(__location__ ": Administrator access required for DsReplicaGetInfo\n"));
753                         security_token_debug(2, dce_call->conn->auth_state.session_info->security_token);
754                         return WERR_DS_DRA_ACCESS_DENIED;
755                 }
756         }
757
758         dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSREPLICAGETINFO,
759                                      &ndr_table_drsuapi, "kccsrv", "DsReplicaGetInfo");
760
761         return WERR_OK;
762 }
763
764
765 /* 
766   DRSUAPI_ADD_SID_HISTORY 
767 */
768 static WERROR dcesrv_DRSUAPI_ADD_SID_HISTORY(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
769                        struct DRSUAPI_ADD_SID_HISTORY *r)
770 {
771         DRSUAPI_UNSUPPORTED(DRSUAPI_ADD_SID_HISTORY);
772 }
773
774 /* 
775   drsuapi_DsGetMemberships2 
776 */
777 static WERROR dcesrv_drsuapi_DsGetMemberships2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
778                        struct drsuapi_DsGetMemberships2 *r)
779 {
780         DRSUAPI_UNSUPPORTED(drsuapi_DsGetMemberships2);
781 }
782
783 /* 
784   DRSUAPI_REPLICA_VERIFY_OBJECTS 
785 */
786 static WERROR dcesrv_DRSUAPI_REPLICA_VERIFY_OBJECTS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
787                        struct DRSUAPI_REPLICA_VERIFY_OBJECTS *r)
788 {
789         DRSUAPI_UNSUPPORTED(DRSUAPI_REPLICA_VERIFY_OBJECTS);
790 }
791
792
793 /* 
794   DRSUAPI_GET_OBJECT_EXISTENCE 
795 */
796 static WERROR dcesrv_DRSUAPI_GET_OBJECT_EXISTENCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
797                        struct DRSUAPI_GET_OBJECT_EXISTENCE *r)
798 {
799         DRSUAPI_UNSUPPORTED(DRSUAPI_GET_OBJECT_EXISTENCE);
800 }
801
802
803 /* 
804   drsuapi_QuerySitesByCost 
805 */
806 static WERROR dcesrv_drsuapi_QuerySitesByCost(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
807                        struct drsuapi_QuerySitesByCost *r)
808 {
809         DRSUAPI_UNSUPPORTED(drsuapi_QuerySitesByCost);
810 }
811
812
813 /* include the generated boilerplate */
814 #include "librpc/gen_ndr/ndr_drsuapi_s.c"