s3: Fix a bad memleak in winbind
[Samba.git] / source4 / rpc_server / drsuapi / dcesrv_drsuapi.c
blob9903f0874633ec8a3e370795f46d36016d0a957f
1 /*
2 Unix SMB/CIFS implementation.
4 endpoint server for the drsuapi pipe
6 Copyright (C) Stefan Metzmacher 2004
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
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.
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.
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/>.
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"
31 /*
32 drsuapi_DsBind
34 static WERROR dcesrv_drsuapi_DsBind(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
35 struct drsuapi_DsBind *r)
37 struct drsuapi_bind_state *b_state;
38 struct dcesrv_handle *handle;
39 struct drsuapi_DsBindInfoCtr *bind_info;
40 struct GUID site_guid;
41 struct ldb_result *site_res;
42 struct ldb_dn *server_site_dn;
43 static const char *site_attrs[] = { "objectGUID", NULL };
44 struct ldb_result *ntds_res;
45 struct ldb_dn *ntds_dn;
46 static const char *ntds_attrs[] = { "ms-DS-ReplicationEpoch", NULL };
47 uint32_t pid;
48 uint32_t repl_epoch;
49 int ret;
51 r->out.bind_info = NULL;
52 ZERO_STRUCTP(r->out.bind_handle);
54 b_state = talloc_zero(mem_ctx, struct drsuapi_bind_state);
55 W_ERROR_HAVE_NO_MEMORY(b_state);
58 * connect to the samdb
60 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);
61 if (!b_state->sam_ctx) {
62 return WERR_FOOBAR;
66 * find out the guid of our own site
68 server_site_dn = samdb_server_site_dn(b_state->sam_ctx, mem_ctx);
69 W_ERROR_HAVE_NO_MEMORY(server_site_dn);
71 ret = ldb_search(b_state->sam_ctx, mem_ctx, &site_res,
72 server_site_dn, LDB_SCOPE_BASE, site_attrs,
73 "(objectClass=*)");
74 if (ret != LDB_SUCCESS) {
75 return WERR_DS_DRA_INTERNAL_ERROR;
77 if (site_res->count != 1) {
78 return WERR_DS_DRA_INTERNAL_ERROR;
80 site_guid = samdb_result_guid(site_res->msgs[0], "objectGUID");
83 * lookup the local servers Replication Epoch
85 ntds_dn = samdb_ntds_settings_dn(b_state->sam_ctx);
86 W_ERROR_HAVE_NO_MEMORY(ntds_dn);
88 ret = ldb_search(b_state->sam_ctx, mem_ctx, &ntds_res,
89 ntds_dn, LDB_SCOPE_BASE, ntds_attrs,
90 "(objectClass=*)");
91 if (ret != LDB_SUCCESS) {
92 return WERR_DS_DRA_INTERNAL_ERROR;
94 if (ntds_res->count != 1) {
95 return WERR_DS_DRA_INTERNAL_ERROR;
97 repl_epoch = samdb_result_uint(ntds_res->msgs[0], "ms-DS-ReplicationEpoch", 0);
100 * The "process identifier" of the client.
101 * According to the WSPP docs, sectin 5.35, this is
102 * for informational and debugging purposes only.
103 * The assignment is implementation specific.
105 pid = 0;
108 * store the clients bind_guid
110 if (r->in.bind_guid) {
111 b_state->remote_bind_guid = *r->in.bind_guid;
115 * store the clients bind_info
117 if (r->in.bind_info) {
118 switch (r->in.bind_info->length) {
119 case 24: {
120 struct drsuapi_DsBindInfo24 *info24;
121 info24 = &r->in.bind_info->info.info24;
122 b_state->remote_info28.supported_extensions = info24->supported_extensions;
123 b_state->remote_info28.site_guid = info24->site_guid;
124 b_state->remote_info28.pid = info24->pid;
125 b_state->remote_info28.repl_epoch = 0;
126 break;
128 case 28:
129 b_state->remote_info28 = r->in.bind_info->info.info28;
130 break;
135 * fill in our local bind info 28
137 b_state->local_info28.supported_extensions = 0;
138 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
139 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
140 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
141 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
142 #if 0 /* we don't support MSZIP compression (only decompression) */
143 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
144 #endif
145 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
146 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
147 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
148 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
149 if (0 /*domain.behavior_version == 2*/) {
150 /* TODO: find out how this is really triggered! */
151 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
153 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
154 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
155 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
156 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
157 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
158 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
159 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
160 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
161 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
162 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_00100000;
163 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
164 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
165 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
166 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
167 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
168 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
169 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
170 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
171 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
172 #if 0 /* we don't support XPRESS compression yet */
173 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
174 #endif
175 b_state->local_info28.site_guid = site_guid;
176 b_state->local_info28.pid = pid;
177 b_state->local_info28.repl_epoch = repl_epoch;
180 * allocate the return bind_info
182 bind_info = talloc(mem_ctx, struct drsuapi_DsBindInfoCtr);
183 W_ERROR_HAVE_NO_MEMORY(bind_info);
185 bind_info->length = 28;
186 bind_info->info.info28 = b_state->local_info28;
189 * allocate a bind handle
191 handle = dcesrv_handle_new(dce_call->context, DRSUAPI_BIND_HANDLE);
192 W_ERROR_HAVE_NO_MEMORY(handle);
193 handle->data = talloc_steal(handle, b_state);
196 * prepare reply
198 r->out.bind_info = bind_info;
199 *r->out.bind_handle = handle->wire_handle;
201 return WERR_OK;
206 drsuapi_DsUnbind
208 static WERROR dcesrv_drsuapi_DsUnbind(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
209 struct drsuapi_DsUnbind *r)
211 struct dcesrv_handle *h;
213 *r->out.bind_handle = *r->in.bind_handle;
215 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
217 talloc_free(h);
219 ZERO_STRUCTP(r->out.bind_handle);
221 return WERR_OK;
226 drsuapi_DsReplicaSync
228 static WERROR dcesrv_drsuapi_DsReplicaSync(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
229 struct drsuapi_DsReplicaSync *r)
231 WERROR status;
233 status = drs_security_level_check(dce_call, "DsReplicaSync");
234 if (!W_ERROR_IS_OK(status)) {
235 return status;
238 dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSREPLICASYNC,
239 &ndr_table_drsuapi,
240 "dreplsrv", "DsReplicaSync");
242 return WERR_OK;
247 drsuapi_DsReplicaAdd
249 static WERROR dcesrv_drsuapi_DsReplicaAdd(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
250 struct drsuapi_DsReplicaAdd *r)
252 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
257 drsuapi_DsReplicaDel
259 static WERROR dcesrv_drsuapi_DsReplicaDel(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
260 struct drsuapi_DsReplicaDel *r)
262 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
267 drsuapi_DsReplicaModify
269 static WERROR dcesrv_drsuapi_DsReplicaMod(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
270 struct drsuapi_DsReplicaMod *r)
272 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
277 DRSUAPI_VERIFY_NAMES
279 static WERROR dcesrv_DRSUAPI_VERIFY_NAMES(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
280 struct DRSUAPI_VERIFY_NAMES *r)
282 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
287 drsuapi_DsGetMemberships
289 static WERROR dcesrv_drsuapi_DsGetMemberships(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
290 struct drsuapi_DsGetMemberships *r)
292 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
297 DRSUAPI_INTER_DOMAIN_MOVE
299 static WERROR dcesrv_DRSUAPI_INTER_DOMAIN_MOVE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
300 struct DRSUAPI_INTER_DOMAIN_MOVE *r)
302 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
307 drsuapi_DsGetNT4ChangeLog
309 static WERROR dcesrv_drsuapi_DsGetNT4ChangeLog(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
310 struct drsuapi_DsGetNT4ChangeLog *r)
312 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
317 drsuapi_DsCrackNames
319 static WERROR dcesrv_drsuapi_DsCrackNames(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
320 struct drsuapi_DsCrackNames *r)
322 WERROR status;
323 struct drsuapi_bind_state *b_state;
324 struct dcesrv_handle *h;
326 *r->out.level_out = r->in.level;
328 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
329 b_state = h->data;
331 r->out.ctr = talloc_zero(mem_ctx, union drsuapi_DsNameCtr);
332 W_ERROR_HAVE_NO_MEMORY(r->out.ctr);
334 switch (r->in.level) {
335 case 1: {
336 struct drsuapi_DsNameCtr1 *ctr1;
337 struct drsuapi_DsNameInfo1 *names;
338 int count;
339 int i;
341 ctr1 = talloc(mem_ctx, struct drsuapi_DsNameCtr1);
342 W_ERROR_HAVE_NO_MEMORY(ctr1);
344 count = r->in.req->req1.count;
345 names = talloc_array(mem_ctx, struct drsuapi_DsNameInfo1, count);
346 W_ERROR_HAVE_NO_MEMORY(names);
348 for (i=0; i < count; i++) {
349 status = DsCrackNameOneName(b_state->sam_ctx, mem_ctx,
350 r->in.req->req1.format_flags,
351 r->in.req->req1.format_offered,
352 r->in.req->req1.format_desired,
353 r->in.req->req1.names[i].str,
354 &names[i]);
355 if (!W_ERROR_IS_OK(status)) {
356 return status;
360 ctr1->count = count;
361 ctr1->array = names;
362 r->out.ctr->ctr1 = ctr1;
364 return WERR_OK;
368 return WERR_UNKNOWN_LEVEL;
372 drsuapi_DsWriteAccountSpn
374 static WERROR dcesrv_drsuapi_DsWriteAccountSpn(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
375 struct drsuapi_DsWriteAccountSpn *r)
377 struct drsuapi_bind_state *b_state;
378 struct dcesrv_handle *h;
380 *r->out.level_out = r->in.level;
382 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
383 b_state = h->data;
385 r->out.res = talloc(mem_ctx, union drsuapi_DsWriteAccountSpnResult);
386 W_ERROR_HAVE_NO_MEMORY(r->out.res);
388 switch (r->in.level) {
389 case 1: {
390 struct drsuapi_DsWriteAccountSpnRequest1 *req;
391 struct ldb_message *msg;
392 int count, i, ret;
393 req = &r->in.req->req1;
394 count = req->count;
396 msg = ldb_msg_new(mem_ctx);
397 if (msg == NULL) {
398 return WERR_NOMEM;
401 msg->dn = ldb_dn_new(msg, b_state->sam_ctx, req->object_dn);
402 if ( ! ldb_dn_validate(msg->dn)) {
403 r->out.res->res1.status = WERR_OK;
404 return WERR_OK;
407 /* construct mods */
408 for (i = 0; i < count; i++) {
409 samdb_msg_add_string(b_state->sam_ctx,
410 msg, msg, "servicePrincipalName",
411 req->spn_names[i].str);
413 for (i=0;i<msg->num_elements;i++) {
414 switch (req->operation) {
415 case DRSUAPI_DS_SPN_OPERATION_ADD:
416 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
417 break;
418 case DRSUAPI_DS_SPN_OPERATION_REPLACE:
419 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
420 break;
421 case DRSUAPI_DS_SPN_OPERATION_DELETE:
422 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
423 break;
427 /* Apply to database */
429 ret = ldb_modify(b_state->sam_ctx, msg);
430 if (ret != 0) {
431 DEBUG(0,("Failed to modify SPNs on %s: %s\n",
432 ldb_dn_get_linearized(msg->dn),
433 ldb_errstring(b_state->sam_ctx)));
434 r->out.res->res1.status = WERR_ACCESS_DENIED;
435 } else {
436 r->out.res->res1.status = WERR_OK;
439 return WERR_OK;
443 return WERR_UNKNOWN_LEVEL;
448 drsuapi_DsRemoveDSServer
450 static WERROR dcesrv_drsuapi_DsRemoveDSServer(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
451 struct drsuapi_DsRemoveDSServer *r)
453 struct drsuapi_bind_state *b_state;
454 struct dcesrv_handle *h;
455 struct ldb_dn *ntds_dn;
456 int ret;
457 bool ok;
458 WERROR status;
460 ZERO_STRUCT(r->out.res);
461 *r->out.level_out = 1;
463 status = drs_security_level_check(dce_call, "DsRemoveDSServer");
464 if (!W_ERROR_IS_OK(status)) {
465 return status;
468 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
469 b_state = h->data;
471 switch (r->in.level) {
472 case 1:
473 ntds_dn = ldb_dn_new(mem_ctx, b_state->sam_ctx, r->in.req->req1.server_dn);
474 W_ERROR_HAVE_NO_MEMORY(ntds_dn);
476 ok = ldb_dn_validate(ntds_dn);
477 if (!ok) {
478 return WERR_FOOBAR;
481 /* TODO: it's likely that we need more checks here */
483 ok = ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings");
484 if (!ok) {
485 return WERR_FOOBAR;
488 if (r->in.req->req1.commit) {
489 ret = ldb_delete(b_state->sam_ctx, ntds_dn);
490 if (ret != LDB_SUCCESS) {
491 return WERR_FOOBAR;
495 return WERR_OK;
496 default:
497 break;
500 return WERR_FOOBAR;
505 DRSUAPI_REMOVE_DS_DOMAIN
507 static WERROR dcesrv_DRSUAPI_REMOVE_DS_DOMAIN(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
508 struct DRSUAPI_REMOVE_DS_DOMAIN *r)
510 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
513 /* Obtain the site name from a server DN */
514 static const char *result_site_name(struct ldb_dn *site_dn)
516 /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
517 const struct ldb_val *val = ldb_dn_get_component_val(site_dn, 2);
518 const char *name = ldb_dn_get_component_name(site_dn, 2);
520 if (!name || (ldb_attr_cmp(name, "cn") != 0)) {
521 /* Ensure this matches the format. This gives us a
522 * bit more confidence that a 'cn' value will be a
523 * ascii string */
524 return NULL;
526 if (val) {
527 return (char *)val->data;
529 return NULL;
533 drsuapi_DsGetDomainControllerInfo
535 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo_1(struct drsuapi_bind_state *b_state,
536 TALLOC_CTX *mem_ctx,
537 struct drsuapi_DsGetDomainControllerInfo *r)
539 struct ldb_dn *sites_dn;
540 struct ldb_result *res;
542 const char *attrs_account_1[] = { "cn", "dnsHostName", NULL };
543 const char *attrs_account_2[] = { "cn", "dnsHostName", "objectGUID", NULL };
545 const char *attrs_none[] = { NULL };
547 const char *attrs_site[] = { "objectGUID", NULL };
549 const char *attrs_ntds[] = { "options", "objectGUID", NULL };
551 const char *attrs_1[] = { "serverReference", "cn", "dnsHostName", NULL };
552 const char *attrs_2[] = { "serverReference", "cn", "dnsHostName", "objectGUID", NULL };
553 const char **attrs;
555 struct drsuapi_DsGetDCInfoCtr1 *ctr1;
556 struct drsuapi_DsGetDCInfoCtr2 *ctr2;
558 int ret, i;
560 *r->out.level_out = r->in.req->req1.level;
561 r->out.ctr = talloc(mem_ctx, union drsuapi_DsGetDCInfoCtr);
562 W_ERROR_HAVE_NO_MEMORY(r->out.ctr);
564 sites_dn = samdb_sites_dn(b_state->sam_ctx, mem_ctx);
565 if (!sites_dn) {
566 return WERR_DS_OBJ_NOT_FOUND;
569 switch (*r->out.level_out) {
570 case -1:
571 /* this level is not like the others */
572 return WERR_UNKNOWN_LEVEL;
573 case 1:
574 attrs = attrs_1;
575 break;
576 case 2:
577 attrs = attrs_2;
578 break;
579 default:
580 return WERR_UNKNOWN_LEVEL;
583 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res, sites_dn, LDB_SCOPE_SUBTREE, attrs,
584 "objectClass=server");
586 if (ret) {
587 DEBUG(1, ("searching for servers in sites DN %s failed: %s\n",
588 ldb_dn_get_linearized(sites_dn), ldb_errstring(b_state->sam_ctx)));
589 return WERR_GENERAL_FAILURE;
592 switch (*r->out.level_out) {
593 case 1:
594 ctr1 = &r->out.ctr->ctr1;
595 ctr1->count = res->count;
596 ctr1->array = talloc_zero_array(mem_ctx,
597 struct drsuapi_DsGetDCInfo1,
598 res->count);
599 for (i=0; i < res->count; i++) {
600 struct ldb_dn *domain_dn;
601 struct ldb_result *res_domain;
602 struct ldb_result *res_account;
603 struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
605 struct ldb_dn *ref_dn
606 = ldb_msg_find_attr_as_dn(b_state->sam_ctx,
607 mem_ctx, res->msgs[i],
608 "serverReference");
610 if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
611 return WERR_NOMEM;
614 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
615 LDB_SCOPE_BASE, attrs_account_1, "objectClass=computer");
616 if (ret == LDB_SUCCESS && res_account->count == 1) {
617 const char *errstr;
618 ctr1->array[i].dns_name
619 = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
620 ctr1->array[i].netbios_name
621 = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
622 ctr1->array[i].computer_dn
623 = ldb_dn_get_linearized(res_account->msgs[0]->dn);
625 /* Determine if this is the PDC */
626 ret = samdb_search_for_parent_domain(b_state->sam_ctx,
627 mem_ctx, res_account->msgs[0]->dn,
628 &domain_dn, &errstr);
630 if (ret == LDB_SUCCESS) {
631 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
632 LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
633 ldb_dn_get_linearized(ntds_dn));
634 if (ret) {
635 return WERR_GENERAL_FAILURE;
637 if (res_domain->count == 1) {
638 ctr1->array[i].is_pdc = true;
642 if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
643 DEBUG(5, ("warning: searching for computer DN %s failed: %s\n",
644 ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
647 /* Look at server DN and extract site component */
648 ctr1->array[i].site_name = result_site_name(res->msgs[i]->dn);
649 ctr1->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
652 ctr1->array[i].is_enabled = true;
655 break;
656 case 2:
657 ctr2 = &r->out.ctr->ctr2;
658 ctr2->count = res->count;
659 ctr2->array = talloc_zero_array(mem_ctx,
660 struct drsuapi_DsGetDCInfo2,
661 res->count);
662 for (i=0; i < res->count; i++) {
663 struct ldb_dn *domain_dn;
664 struct ldb_result *res_domain;
665 struct ldb_result *res_account;
666 struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
667 struct ldb_result *res_ntds;
668 struct ldb_dn *site_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
669 struct ldb_result *res_site;
670 struct ldb_dn *ref_dn
671 = ldb_msg_find_attr_as_dn(b_state->sam_ctx,
672 mem_ctx, res->msgs[i],
673 "serverReference");
675 if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
676 return WERR_NOMEM;
679 /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
680 if (!site_dn || !ldb_dn_remove_child_components(site_dn, 2)) {
681 return WERR_NOMEM;
684 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_ntds, ntds_dn,
685 LDB_SCOPE_BASE, attrs_ntds, "objectClass=nTDSDSA");
686 if (ret == LDB_SUCCESS && res_ntds->count == 1) {
687 ctr2->array[i].is_gc
688 = (ldb_msg_find_attr_as_int(res_ntds->msgs[0], "options", 0) == 1);
689 ctr2->array[i].ntds_guid
690 = samdb_result_guid(res_ntds->msgs[0], "objectGUID");
691 ctr2->array[i].ntds_dn = ldb_dn_get_linearized(res_ntds->msgs[0]->dn);
693 if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
694 DEBUG(5, ("warning: searching for NTDS DN %s failed: %s\n",
695 ldb_dn_get_linearized(ntds_dn), ldb_errstring(b_state->sam_ctx)));
698 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_site, site_dn,
699 LDB_SCOPE_BASE, attrs_site, "objectClass=site");
700 if (ret == LDB_SUCCESS && res_site->count == 1) {
701 ctr2->array[i].site_guid
702 = samdb_result_guid(res_site->msgs[0], "objectGUID");
703 ctr2->array[i].site_dn = ldb_dn_get_linearized(res_site->msgs[0]->dn);
705 if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
706 DEBUG(5, ("warning: searching for site DN %s failed: %s\n",
707 ldb_dn_get_linearized(site_dn), ldb_errstring(b_state->sam_ctx)));
710 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
711 LDB_SCOPE_BASE, attrs_account_2, "objectClass=computer");
712 if (ret == LDB_SUCCESS && res_account->count == 1) {
713 const char *errstr;
714 ctr2->array[i].dns_name
715 = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
716 ctr2->array[i].netbios_name
717 = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
718 ctr2->array[i].computer_dn = ldb_dn_get_linearized(res_account->msgs[0]->dn);
719 ctr2->array[i].computer_guid
720 = samdb_result_guid(res_account->msgs[0], "objectGUID");
722 /* Determine if this is the PDC */
723 ret = samdb_search_for_parent_domain(b_state->sam_ctx,
724 mem_ctx, res_account->msgs[0]->dn,
725 &domain_dn, &errstr);
727 if (ret == LDB_SUCCESS) {
728 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
729 LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
730 ldb_dn_get_linearized(ntds_dn));
731 if (ret == LDB_SUCCESS && res_domain->count == 1) {
732 ctr2->array[i].is_pdc = true;
734 if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
735 DEBUG(5, ("warning: searching for domain DN %s failed: %s\n",
736 ldb_dn_get_linearized(domain_dn), ldb_errstring(b_state->sam_ctx)));
740 if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
741 DEBUG(5, ("warning: searching for computer account DN %s failed: %s\n",
742 ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
745 /* Look at server DN and extract site component */
746 ctr2->array[i].site_name = result_site_name(res->msgs[i]->dn);
747 ctr2->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
748 ctr2->array[i].server_guid
749 = samdb_result_guid(res->msgs[i], "objectGUID");
751 ctr2->array[i].is_enabled = true;
754 break;
756 return WERR_OK;
760 drsuapi_DsGetDomainControllerInfo
762 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
763 struct drsuapi_DsGetDomainControllerInfo *r)
765 struct dcesrv_handle *h;
766 struct drsuapi_bind_state *b_state;
767 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
768 b_state = h->data;
770 switch (r->in.level) {
771 case 1:
772 return dcesrv_drsuapi_DsGetDomainControllerInfo_1(b_state, mem_ctx, r);
775 return WERR_UNKNOWN_LEVEL;
781 drsuapi_DsExecuteKCC
783 static WERROR dcesrv_drsuapi_DsExecuteKCC(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
784 struct drsuapi_DsExecuteKCC *r)
786 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
791 drsuapi_DsReplicaGetInfo
793 static WERROR dcesrv_drsuapi_DsReplicaGetInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
794 struct drsuapi_DsReplicaGetInfo *r)
796 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
801 DRSUAPI_ADD_SID_HISTORY
803 static WERROR dcesrv_DRSUAPI_ADD_SID_HISTORY(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
804 struct DRSUAPI_ADD_SID_HISTORY *r)
806 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
810 drsuapi_DsGetMemberships2
812 static WERROR dcesrv_drsuapi_DsGetMemberships2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
813 struct drsuapi_DsGetMemberships2 *r)
815 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
819 DRSUAPI_REPLICA_VERIFY_OBJECTS
821 static WERROR dcesrv_DRSUAPI_REPLICA_VERIFY_OBJECTS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
822 struct DRSUAPI_REPLICA_VERIFY_OBJECTS *r)
824 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
829 DRSUAPI_GET_OBJECT_EXISTENCE
831 static WERROR dcesrv_DRSUAPI_GET_OBJECT_EXISTENCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
832 struct DRSUAPI_GET_OBJECT_EXISTENCE *r)
834 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
839 drsuapi_QuerySitesByCost
841 static WERROR dcesrv_drsuapi_QuerySitesByCost(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
842 struct drsuapi_QuerySitesByCost *r)
844 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
848 /* include the generated boilerplate */
849 #include "librpc/gen_ndr/ndr_drsuapi_s.c"