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