r26296: Store loadparm context in DCE/RPC server context.
[jelmer/samba4-debian.git] / source / 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 "rpc_server/drsuapi/dcesrv_drsuapi.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "lib/ldb/include/ldb_errors.h"
30 #include "param/param.h"
31
32 /* 
33   drsuapi_DsBind 
34 */
35 static WERROR dcesrv_drsuapi_DsBind(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
36                        struct drsuapi_DsBind *r)
37 {
38         struct drsuapi_bind_state *b_state;
39         struct dcesrv_handle *handle;
40         struct drsuapi_DsBindInfoCtr *bind_info;
41         struct GUID site_guid;
42         struct ldb_result *site_res;
43         struct ldb_dn *server_site_dn;
44         static const char *site_attrs[] = { "objectGUID", NULL };
45         struct ldb_result *ntds_res;
46         struct ldb_dn *ntds_dn;
47         static const char *ntds_attrs[] = { "ms-DS-ReplicationEpoch", NULL };
48         uint32_t u1;
49         uint32_t repl_epoch;
50         int ret;
51
52         r->out.bind_info = NULL;
53         ZERO_STRUCTP(r->out.bind_handle);
54
55         b_state = talloc_zero(mem_ctx, struct drsuapi_bind_state);
56         W_ERROR_HAVE_NO_MEMORY(b_state);
57
58         /*
59          * connect to the samdb
60          */
61         b_state->sam_ctx = samdb_connect(b_state, dce_call->conn->dce_ctx->lp_ctx, dce_call->conn->auth_state.session_info); 
62         if (!b_state->sam_ctx) {
63                 return WERR_FOOBAR;
64         }
65
66         /*
67          * find out the guid of our own site
68          */
69         server_site_dn = samdb_server_site_dn(b_state->sam_ctx, mem_ctx);
70         W_ERROR_HAVE_NO_MEMORY(server_site_dn);
71
72         ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &site_res,
73                                  server_site_dn, LDB_SCOPE_BASE, site_attrs,
74                                  "(objectClass=*)");
75         if (ret != LDB_SUCCESS) {
76                 return WERR_DS_DRA_INTERNAL_ERROR;
77         }
78         if (site_res->count != 1) {
79                 return WERR_DS_DRA_INTERNAL_ERROR;
80         }
81         site_guid = samdb_result_guid(site_res->msgs[0], "objectGUID");
82
83         /*
84          * lookup the local servers Replication Epoch
85          */
86         ntds_dn = samdb_ntds_settings_dn(b_state->sam_ctx);
87         W_ERROR_HAVE_NO_MEMORY(ntds_dn);
88
89         ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &ntds_res,
90                                  ntds_dn, LDB_SCOPE_BASE, ntds_attrs,
91                                  "(objectClass=*)");
92         if (ret != LDB_SUCCESS) {
93                 return WERR_DS_DRA_INTERNAL_ERROR;
94         }
95         if (ntds_res->count != 1) {
96                 return WERR_DS_DRA_INTERNAL_ERROR;
97         }
98         repl_epoch = samdb_result_uint(ntds_res->msgs[0], "ms-DS-ReplicationEpoch", 0);
99
100         /*
101          * TODO: find out what this is...
102          */
103         u1 = 0;
104
105         /*
106          * store the clients bind_guid
107          */
108         if (r->in.bind_guid) {
109                 b_state->remote_bind_guid = *r->in.bind_guid;
110         }
111
112         /*
113          * store the clients bind_info
114          */
115         if (r->in.bind_info) {
116                 switch (r->in.bind_info->length) {
117                 case 24: {
118                         struct drsuapi_DsBindInfo24 *info24;
119                         info24 = &r->in.bind_info->info.info24;
120                         b_state->remote_info28.supported_extensions     = info24->supported_extensions;
121                         b_state->remote_info28.site_guid                = info24->site_guid;
122                         b_state->remote_info28.u1                       = info24->u1;
123                         b_state->remote_info28.repl_epoch               = 0;
124                         break;
125                 }
126                 case 28:
127                         b_state->remote_info28 = r->in.bind_info->info.info28;
128                         break;
129                 }
130         }
131
132         /*
133          * fill in our local bind info 28
134          */
135         b_state->local_info28.supported_extensions      = 0;
136         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
137         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
138         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
139         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
140 #if 0 /* we don't support MSZIP compression (only decompression) */
141         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
142 #endif
143         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
144         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
145         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
146         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
147         if (0 /*domain.behavior_version == 2*/) {
148                 /* TODO: find out how this is really triggered! */
149                 b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
150         }
151         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
152         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
153         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
154         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
155         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
156         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
157         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
158         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
159         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
160         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_00100000;
161         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
162         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
163         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
164         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
165         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
166         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
167         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
168         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
169         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
170 #if 0 /* we don't support XPRESS compression yet */
171         b_state->local_info28.supported_extensions      |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
172 #endif
173         b_state->local_info28.site_guid                 = site_guid;
174         b_state->local_info28.u1                                = u1;
175         b_state->local_info28.repl_epoch                        = repl_epoch;
176
177         /*
178          * allocate the return bind_info
179          */
180         bind_info = talloc(mem_ctx, struct drsuapi_DsBindInfoCtr);
181         W_ERROR_HAVE_NO_MEMORY(bind_info);
182
183         bind_info->length       = 28;
184         bind_info->info.info28  = b_state->local_info28;
185
186         /*
187          * allocate a bind handle
188          */
189         handle = dcesrv_handle_new(dce_call->context, DRSUAPI_BIND_HANDLE);
190         W_ERROR_HAVE_NO_MEMORY(handle);
191         handle->data = talloc_steal(handle, b_state);
192
193         /*
194          * prepare reply
195          */
196         r->out.bind_info = bind_info;
197         *r->out.bind_handle = handle->wire_handle;
198
199         return WERR_OK;
200 }
201
202
203 /* 
204   drsuapi_DsUnbind 
205 */
206 static WERROR dcesrv_drsuapi_DsUnbind(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
207                                struct drsuapi_DsUnbind *r)
208 {
209         struct dcesrv_handle *h;
210
211         *r->out.bind_handle = *r->in.bind_handle;
212
213         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
214
215         talloc_free(h);
216
217         ZERO_STRUCTP(r->out.bind_handle);
218
219         return WERR_OK;
220 }
221
222
223 /* 
224   drsuapi_DsReplicaSync 
225 */
226 static WERROR dcesrv_drsuapi_DsReplicaSync(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
227                        struct drsuapi_DsReplicaSync *r)
228 {
229         /* TODO: implement this call correct!
230          *       for now we just say yes,
231          *       because we have no output parameter
232          */
233         return WERR_OK;
234 }
235
236
237 /* 
238   drsuapi_DsGetNCChanges
239 */
240 static WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
241                        struct drsuapi_DsGetNCChanges *r)
242 {
243         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
244 }
245
246
247 /* 
248   drsuapi_DsReplicaUpdateRefs
249 */
250 static WERROR dcesrv_drsuapi_DsReplicaUpdateRefs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
251                        struct drsuapi_DsReplicaUpdateRefs *r)
252 {
253         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
254 }
255
256
257 /* 
258   DRSUAPI_REPLICA_ADD 
259 */
260 static WERROR dcesrv_DRSUAPI_REPLICA_ADD(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
261                        struct DRSUAPI_REPLICA_ADD *r)
262 {
263         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
264 }
265
266
267 /* 
268   DRSUAPI_REPLICA_DEL 
269 */
270 static WERROR dcesrv_DRSUAPI_REPLICA_DEL(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
271                        struct DRSUAPI_REPLICA_DEL *r)
272 {
273         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
274 }
275
276
277 /* 
278   DRSUAPI_REPLICA_MODIFY 
279 */
280 static WERROR dcesrv_DRSUAPI_REPLICA_MODIFY(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
281                        struct DRSUAPI_REPLICA_MODIFY *r)
282 {
283         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
284 }
285
286
287 /* 
288   DRSUAPI_VERIFY_NAMES 
289 */
290 static WERROR dcesrv_DRSUAPI_VERIFY_NAMES(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
291                        struct DRSUAPI_VERIFY_NAMES *r)
292 {
293         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
294 }
295
296
297 /* 
298   drsuapi_DsGetMemberships 
299 */
300 static WERROR dcesrv_drsuapi_DsGetMemberships(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
301                        struct drsuapi_DsGetMemberships *r)
302 {
303         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
304 }
305
306
307 /* 
308   DRSUAPI_INTER_DOMAIN_MOVE 
309 */
310 static WERROR dcesrv_DRSUAPI_INTER_DOMAIN_MOVE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
311                        struct DRSUAPI_INTER_DOMAIN_MOVE *r)
312 {
313         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
314 }
315
316
317 /* 
318   drsuapi_DsGetNT4ChangeLog 
319 */
320 static WERROR dcesrv_drsuapi_DsGetNT4ChangeLog(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
321                        struct drsuapi_DsGetNT4ChangeLog *r)
322 {
323         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
324 }
325
326
327 /* 
328   drsuapi_DsCrackNames 
329 */
330 WERROR dcesrv_drsuapi_DsCrackNames(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
331                             struct drsuapi_DsCrackNames *r)
332 {
333         WERROR status;
334         struct drsuapi_bind_state *b_state;
335         struct dcesrv_handle *h;
336
337         r->out.level = r->in.level;
338         ZERO_STRUCT(r->out.ctr);
339
340         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
341         b_state = h->data;
342
343         switch (r->in.level) {
344                 case 1: {
345                         struct drsuapi_DsNameCtr1 *ctr1;
346                         struct drsuapi_DsNameInfo1 *names;
347                         int count;
348                         int i;
349
350                         ctr1 = talloc(mem_ctx, struct drsuapi_DsNameCtr1);
351                         W_ERROR_HAVE_NO_MEMORY(ctr1);
352
353                         count = r->in.req.req1.count;
354                         names = talloc_array(mem_ctx, struct drsuapi_DsNameInfo1, count);
355                         W_ERROR_HAVE_NO_MEMORY(names);
356
357                         for (i=0; i < count; i++) {
358                                 status = DsCrackNameOneName(b_state->sam_ctx, mem_ctx,
359                                                             r->in.req.req1.format_flags,
360                                                             r->in.req.req1.format_offered,
361                                                             r->in.req.req1.format_desired,
362                                                             r->in.req.req1.names[i].str,
363                                                             &names[i]);
364                                 if (!W_ERROR_IS_OK(status)) {
365                                         return status;
366                                 }
367                         }
368
369                         ctr1->count = count;
370                         ctr1->array = names;
371                         r->out.ctr.ctr1 = ctr1;
372
373                         return WERR_OK;
374                 }
375         }
376         
377         return WERR_UNKNOWN_LEVEL;
378 }
379
380 /* 
381   drsuapi_DsWriteAccountSpn 
382 */
383 static WERROR dcesrv_drsuapi_DsWriteAccountSpn(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
384                        struct drsuapi_DsWriteAccountSpn *r)
385 {
386         struct drsuapi_bind_state *b_state;
387         struct dcesrv_handle *h;
388
389         r->out.level = r->in.level;
390
391         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
392         b_state = h->data;
393
394         switch (r->in.level) {
395                 case 1: {
396                         struct drsuapi_DsWriteAccountSpnRequest1 *req;
397                         struct ldb_message *msg;
398                         int count, i, ret;
399                         req = &r->in.req.req1;
400                         count = req->count;
401
402                         msg = ldb_msg_new(mem_ctx);
403                         if (msg == NULL) {
404                                 return WERR_NOMEM;
405                         }
406
407                         msg->dn = ldb_dn_new(msg, b_state->sam_ctx, req->object_dn);
408                         if ( ! ldb_dn_validate(msg->dn)) {
409                                 r->out.res.res1.status = WERR_OK;
410                                 return WERR_OK;
411                         }
412                         
413                         /* construct mods */
414                         for (i = 0; i < count; i++) {
415                                 samdb_msg_add_string(b_state->sam_ctx, 
416                                                      msg, msg, "servicePrincipalName",
417                                                      req->spn_names[i].str);
418                         }
419                         for (i=0;i<msg->num_elements;i++) {
420                                 switch (req->operation) {
421                                 case DRSUAPI_DS_SPN_OPERATION_ADD:
422                                         msg->elements[i].flags = LDB_FLAG_MOD_ADD;
423                                         break;
424                                 case DRSUAPI_DS_SPN_OPERATION_REPLACE:
425                                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
426                                         break;
427                                 case DRSUAPI_DS_SPN_OPERATION_DELETE:
428                                         msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
429                                         break;
430                                 }
431                         }
432    
433                         /* Apply to database */
434
435                         ret = ldb_modify(b_state->sam_ctx, msg);
436                         if (ret != 0) {
437                                 DEBUG(0,("Failed to modify SPNs on %s: %s\n",
438                                          ldb_dn_get_linearized(msg->dn), 
439                                          ldb_errstring(b_state->sam_ctx)));
440                                 r->out.res.res1.status = WERR_ACCESS_DENIED;
441                         } else {
442                                 r->out.res.res1.status = WERR_OK;
443                         }
444
445                         return WERR_OK;
446                 }
447         }
448         
449         return WERR_UNKNOWN_LEVEL;
450 }
451
452
453 /* 
454   drsuapi_DsRemoveDSServer
455 */
456 static WERROR dcesrv_drsuapi_DsRemoveDSServer(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
457                                        struct drsuapi_DsRemoveDSServer *r)
458 {
459         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
460 }
461
462
463 /* 
464   DRSUAPI_REMOVE_DS_DOMAIN 
465 */
466 static WERROR dcesrv_DRSUAPI_REMOVE_DS_DOMAIN(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
467                        struct DRSUAPI_REMOVE_DS_DOMAIN *r)
468 {
469         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
470 }
471
472 /* Obtain the site name from a server DN */
473 const char *result_site_name(struct ldb_dn *site_dn)
474 {
475         /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
476         const struct ldb_val *val = ldb_dn_get_component_val(site_dn, 2);
477         const char *name = ldb_dn_get_component_name(site_dn, 2);
478
479         if (!name || (ldb_attr_cmp(name, "cn") != 0)) {
480                 /* Ensure this matches the format.  This gives us a
481                  * bit more confidence that a 'cn' value will be a
482                  * ascii string */
483                 return NULL;
484         }
485         if (val) {
486                 return (char *)val->data;
487         }
488         return NULL;
489 }
490
491 /* 
492   drsuapi_DsGetDomainControllerInfo 
493 */
494 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo_1(struct drsuapi_bind_state *b_state, 
495                                                 TALLOC_CTX *mem_ctx,
496                                                 struct drsuapi_DsGetDomainControllerInfo *r)
497 {
498         struct ldb_dn *sites_dn;
499         struct ldb_result *res;
500
501         const char *attrs_account_1[] = { "cn", "dnsHostName", NULL };
502         const char *attrs_account_2[] = { "cn", "dnsHostName", "objectGUID", NULL };
503
504         const char *attrs_none[] = { NULL };
505
506         const char *attrs_site[] = { "objectGUID", NULL };
507
508         const char *attrs_ntds[] = { "options", "objectGUID", NULL };
509
510         const char *attrs_1[] = { "serverReference", "cn", "dnsHostName", NULL };
511         const char *attrs_2[] = { "serverReference", "cn", "dnsHostName", "objectGUID", NULL };
512         const char **attrs;
513
514         struct drsuapi_DsGetDCInfoCtr1 *ctr1;
515         struct drsuapi_DsGetDCInfoCtr2 *ctr2;
516
517         int ret, i;
518
519         r->out.level_out = r->in.req.req1.level;
520
521         sites_dn = samdb_sites_dn(b_state->sam_ctx, mem_ctx);
522         if (!sites_dn) {
523                 return WERR_DS_OBJ_NOT_FOUND;
524         }
525
526         switch (r->out.level_out) {
527         case -1:
528                 /* this level is not like the others */
529                 return WERR_UNKNOWN_LEVEL;
530         case 1:
531                 attrs = attrs_1;
532                 break;
533         case 2:
534                 attrs = attrs_2;
535                 break;
536         default:
537                 return WERR_UNKNOWN_LEVEL;
538         }
539
540         ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res, sites_dn, LDB_SCOPE_SUBTREE, attrs, 
541                                  "objectClass=server");
542         
543         if (ret) {
544                 DEBUG(1, ("searching for servers in sites DN %s failed: %s\n", 
545                           ldb_dn_get_linearized(sites_dn), ldb_errstring(b_state->sam_ctx)));
546                 return WERR_GENERAL_FAILURE;
547         }
548
549         switch (r->out.level_out) {
550         case 1:
551                 ctr1 = &r->out.ctr.ctr1;
552                 ctr1->count = res->count;
553                 ctr1->array = talloc_zero_array(mem_ctx, 
554                                                 struct drsuapi_DsGetDCInfo1, 
555                                                 res->count);
556                 for (i=0; i < res->count; i++) {
557                         struct ldb_dn *domain_dn;
558                         struct ldb_result *res_domain;
559                         struct ldb_result *res_account;
560                         struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
561                         
562                         struct ldb_dn *ref_dn
563                                 = ldb_msg_find_attr_as_dn(b_state->sam_ctx, 
564                                                           mem_ctx, res->msgs[i], 
565                                                           "serverReference");
566
567                         if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
568                                 return WERR_NOMEM;
569                         }
570
571                         ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res_account, ref_dn, 
572                                                  LDB_SCOPE_BASE, attrs_account_1, "objectClass=computer");
573                         if (ret == LDB_SUCCESS && res_account->count == 1) {
574                                 const char *errstr;
575                                 ctr1->array[i].dns_name
576                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
577                                 ctr1->array[i].netbios_name
578                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
579                                 ctr1->array[i].computer_dn
580                                         = ldb_dn_get_linearized(res_account->msgs[0]->dn);
581
582                                 /* Determine if this is the PDC */
583                                 ret = samdb_search_for_parent_domain(b_state->sam_ctx, 
584                                                                      mem_ctx, res_account->msgs[0]->dn,
585                                                                      &domain_dn, &errstr);
586                                 
587                                 if (ret == LDB_SUCCESS) {
588                                         ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn, 
589                                                                  LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
590                                                                  ldb_dn_get_linearized(ntds_dn));
591                                         if (ret) {
592                                                 return WERR_GENERAL_FAILURE;
593                                         }
594                                         if (res_domain->count == 1) {
595                                                 ctr1->array[i].is_pdc = true;
596                                         }
597                                 }
598                         }
599                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
600                                 DEBUG(5, ("warning: searching for computer DN %s failed: %s\n", 
601                                           ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
602                         }
603
604                         /* Look at server DN and extract site component */
605                         ctr1->array[i].site_name = result_site_name(res->msgs[i]->dn);
606                         ctr1->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
607
608
609                         ctr1->array[i].is_enabled = true;
610
611                 }
612                 break;
613         case 2:
614                 ctr2 = &r->out.ctr.ctr2;
615                 ctr2->count = res->count;
616                 ctr2->array = talloc_zero_array(mem_ctx, 
617                                                  struct drsuapi_DsGetDCInfo2, 
618                                                  res->count);
619                 for (i=0; i < res->count; i++) {
620                         struct ldb_dn *domain_dn;
621                         struct ldb_result *res_domain;
622                         struct ldb_result *res_account;
623                         struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
624                         struct ldb_result *res_ntds;
625                         struct ldb_dn *site_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
626                         struct ldb_result *res_site;
627                         struct ldb_dn *ref_dn
628                                 = ldb_msg_find_attr_as_dn(b_state->sam_ctx, 
629                                                           mem_ctx, res->msgs[i], 
630                                                           "serverReference");
631
632                         if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
633                                 return WERR_NOMEM;
634                         }
635
636                         /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
637                         if (!site_dn || !ldb_dn_remove_child_components(site_dn, 2)) {
638                                 return WERR_NOMEM;
639                         }
640
641                         ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res_ntds, ntds_dn, 
642                                                  LDB_SCOPE_BASE, attrs_ntds, "objectClass=nTDSDSA");
643                         if (ret == LDB_SUCCESS && res_ntds->count == 1) {
644                                 ctr2->array[i].is_gc
645                                         = (ldb_msg_find_attr_as_int(res_ntds->msgs[0], "options", 0) == 1);
646                                 ctr2->array[i].ntds_guid 
647                                         = samdb_result_guid(res_ntds->msgs[0], "objectGUID");
648                                 ctr2->array[i].ntds_dn = ldb_dn_get_linearized(res_ntds->msgs[0]->dn);
649                         }
650                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
651                                 DEBUG(5, ("warning: searching for NTDS DN %s failed: %s\n", 
652                                           ldb_dn_get_linearized(ntds_dn), ldb_errstring(b_state->sam_ctx)));
653                         }
654
655                         ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res_site, site_dn, 
656                                                  LDB_SCOPE_BASE, attrs_site, "objectClass=site");
657                         if (ret == LDB_SUCCESS && res_site->count == 1) {
658                                 ctr2->array[i].site_guid 
659                                         = samdb_result_guid(res_site->msgs[0], "objectGUID");
660                                 ctr2->array[i].site_dn = ldb_dn_get_linearized(res_site->msgs[0]->dn);
661                         }
662                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
663                                 DEBUG(5, ("warning: searching for site DN %s failed: %s\n", 
664                                           ldb_dn_get_linearized(site_dn), ldb_errstring(b_state->sam_ctx)));
665                         }
666
667                         ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res_account, ref_dn, 
668                                                  LDB_SCOPE_BASE, attrs_account_2, "objectClass=computer");
669                         if (ret == LDB_SUCCESS && res_account->count == 1) {
670                                 const char *errstr;
671                                 ctr2->array[i].dns_name
672                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
673                                 ctr2->array[i].netbios_name
674                                         = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
675                                 ctr2->array[i].computer_dn = ldb_dn_get_linearized(res_account->msgs[0]->dn);
676                                 ctr2->array[i].computer_guid 
677                                         = samdb_result_guid(res_account->msgs[0], "objectGUID");
678
679                                 /* Determine if this is the PDC */
680                                 ret = samdb_search_for_parent_domain(b_state->sam_ctx, 
681                                                                      mem_ctx, res_account->msgs[0]->dn,
682                                                                      &domain_dn, &errstr);
683                                 
684                                 if (ret == LDB_SUCCESS) {
685                                         ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn, 
686                                                                  LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
687                                                                  ldb_dn_get_linearized(ntds_dn));
688                                         if (ret == LDB_SUCCESS && res_domain->count == 1) {
689                                                 ctr2->array[i].is_pdc = true;
690                                         }
691                                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
692                                                 DEBUG(5, ("warning: searching for domain DN %s failed: %s\n", 
693                                                           ldb_dn_get_linearized(domain_dn), ldb_errstring(b_state->sam_ctx)));
694                                         }
695                                 }
696                         }
697                         if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
698                                 DEBUG(5, ("warning: searching for computer account DN %s failed: %s\n", 
699                                           ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
700                         }
701
702                         /* Look at server DN and extract site component */
703                         ctr2->array[i].site_name = result_site_name(res->msgs[i]->dn);
704                         ctr2->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
705                         ctr2->array[i].server_guid 
706                                 = samdb_result_guid(res->msgs[i], "objectGUID");
707
708                         ctr2->array[i].is_enabled = true;
709
710                 }
711                 break;
712         }
713         return WERR_OK;
714 }
715
716 /* 
717   drsuapi_DsGetDomainControllerInfo 
718 */
719 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
720                                                 struct drsuapi_DsGetDomainControllerInfo *r)
721 {
722         struct dcesrv_handle *h;
723         struct drsuapi_bind_state *b_state;     
724         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
725         b_state = h->data;
726
727         switch (r->in.level) {
728         case 1:
729                 return dcesrv_drsuapi_DsGetDomainControllerInfo_1(b_state, mem_ctx, r);
730         }
731
732         return WERR_UNKNOWN_LEVEL;
733 }
734
735
736 /* 
737   drsuapi_DsAddEntry
738 */
739 static WERROR dcesrv_drsuapi_DsAddEntry(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
740                        struct drsuapi_DsAddEntry *r)
741 {
742         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
743 }
744
745
746 /* 
747   DRSUAPI_EXECUTE_KCC 
748 */
749 static WERROR dcesrv_DRSUAPI_EXECUTE_KCC(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
750                        struct DRSUAPI_EXECUTE_KCC *r)
751 {
752         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
753 }
754
755
756 /* 
757   drsuapi_DsReplicaGetInfo 
758 */
759 static WERROR dcesrv_drsuapi_DsReplicaGetInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
760                        struct drsuapi_DsReplicaGetInfo *r)
761 {
762         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
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         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
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         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
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         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
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         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
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         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
811 }
812
813
814 /* include the generated boilerplate */
815 #include "librpc/gen_ndr/ndr_drsuapi_s.c"