smbcontrol: Simplify do_winbind_offline
[Samba.git] / source4 / rpc_server / drsuapi / dcesrv_drsuapi.c
blob879f63f6df5c2f2ce05f86c1f42ffca381cf0bcd
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 "dsdb/common/util.h"
29 #include "libcli/security/security.h"
30 #include "libcli/security/session.h"
31 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
32 #include "auth/auth.h"
33 #include "param/param.h"
34 #include "lib/messaging/irpc.h"
36 #define DRSUAPI_UNSUPPORTED(fname) do { \
37 DEBUG(1,(__location__ ": Unsupported DRS call %s\n", #fname)); \
38 if (DEBUGLVL(2)) NDR_PRINT_IN_DEBUG(fname, r); \
39 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR); \
40 } while (0)
42 /*
43 drsuapi_DsBind
45 static WERROR dcesrv_drsuapi_DsBind(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
46 struct drsuapi_DsBind *r)
48 struct drsuapi_bind_state *b_state;
49 struct dcesrv_handle *handle;
50 struct drsuapi_DsBindInfoCtr *bind_info;
51 struct GUID site_guid;
52 struct ldb_result *site_res;
53 struct ldb_dn *server_site_dn;
54 static const char *site_attrs[] = { "objectGUID", NULL };
55 struct ldb_result *ntds_res;
56 struct ldb_dn *ntds_dn;
57 static const char *ntds_attrs[] = { "ms-DS-ReplicationEpoch", NULL };
58 uint32_t pid;
59 uint32_t repl_epoch;
60 int ret;
61 struct auth_session_info *auth_info;
62 WERROR werr;
63 bool connected_as_system = false;
65 r->out.bind_info = NULL;
66 ZERO_STRUCTP(r->out.bind_handle);
68 b_state = talloc_zero(mem_ctx, struct drsuapi_bind_state);
69 W_ERROR_HAVE_NO_MEMORY(b_state);
71 /* if this is a DC connecting, give them system level access */
72 werr = drs_security_level_check(dce_call, NULL, SECURITY_DOMAIN_CONTROLLER, NULL);
73 if (W_ERROR_IS_OK(werr)) {
74 DEBUG(3,(__location__ ": doing DsBind with system_session\n"));
75 auth_info = system_session(dce_call->conn->dce_ctx->lp_ctx);
76 connected_as_system = true;
77 } else {
78 auth_info = dce_call->conn->auth_state.session_info;
82 * connect to the samdb
84 b_state->sam_ctx = samdb_connect(b_state, dce_call->event_ctx,
85 dce_call->conn->dce_ctx->lp_ctx, auth_info, 0);
86 if (!b_state->sam_ctx) {
87 return WERR_FOOBAR;
90 if (connected_as_system) {
91 b_state->sam_ctx_system = b_state->sam_ctx;
92 } else {
93 /* an RODC also needs system samdb access for secret
94 attribute replication */
95 werr = drs_security_level_check(dce_call, NULL, SECURITY_RO_DOMAIN_CONTROLLER,
96 samdb_domain_sid(b_state->sam_ctx));
97 if (W_ERROR_IS_OK(werr)) {
98 b_state->sam_ctx_system = samdb_connect(b_state, dce_call->event_ctx,
99 dce_call->conn->dce_ctx->lp_ctx,
100 system_session(dce_call->conn->dce_ctx->lp_ctx), 0);
101 if (!b_state->sam_ctx_system) {
102 return WERR_FOOBAR;
108 * find out the guid of our own site
110 server_site_dn = samdb_server_site_dn(b_state->sam_ctx, mem_ctx);
111 W_ERROR_HAVE_NO_MEMORY(server_site_dn);
113 ret = ldb_search(b_state->sam_ctx, mem_ctx, &site_res,
114 server_site_dn, LDB_SCOPE_BASE, site_attrs,
115 "(objectClass=*)");
116 if (ret != LDB_SUCCESS) {
117 return WERR_DS_DRA_INTERNAL_ERROR;
119 if (site_res->count != 1) {
120 return WERR_DS_DRA_INTERNAL_ERROR;
122 site_guid = samdb_result_guid(site_res->msgs[0], "objectGUID");
125 * lookup the local servers Replication Epoch
127 ntds_dn = samdb_ntds_settings_dn(b_state->sam_ctx, mem_ctx);
128 W_ERROR_HAVE_NO_MEMORY(ntds_dn);
130 ret = ldb_search(b_state->sam_ctx, mem_ctx, &ntds_res,
131 ntds_dn, LDB_SCOPE_BASE, ntds_attrs,
132 "(objectClass=*)");
133 if (ret != LDB_SUCCESS) {
134 return WERR_DS_DRA_INTERNAL_ERROR;
136 if (ntds_res->count != 1) {
137 return WERR_DS_DRA_INTERNAL_ERROR;
139 repl_epoch = ldb_msg_find_attr_as_uint(ntds_res->msgs[0],
140 "ms-DS-ReplicationEpoch", 0);
143 * The "process identifier" of the client.
144 * According to the WSPP docs, sectin 5.35, this is
145 * for informational and debugging purposes only.
146 * The assignment is implementation specific.
148 pid = 0;
151 * store the clients bind_guid
153 if (r->in.bind_guid) {
154 b_state->remote_bind_guid = *r->in.bind_guid;
158 * store the clients bind_info
160 if (r->in.bind_info) {
161 switch (r->in.bind_info->length) {
162 case 24: {
163 struct drsuapi_DsBindInfo24 *info24;
164 info24 = &r->in.bind_info->info.info24;
165 b_state->remote_info28.supported_extensions = info24->supported_extensions;
166 b_state->remote_info28.site_guid = info24->site_guid;
167 b_state->remote_info28.pid = info24->pid;
168 b_state->remote_info28.repl_epoch = 0;
169 break;
171 case 28:
172 b_state->remote_info28 = r->in.bind_info->info.info28;
173 break;
178 * fill in our local bind info 28
180 b_state->local_info28.supported_extensions = 0;
181 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
182 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
183 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
184 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
185 #if 0 /* we don't support MSZIP compression (only decompression) */
186 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
187 #endif
188 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
189 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
190 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
191 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
192 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
193 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
194 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
195 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
196 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
197 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
198 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
199 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
200 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
201 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
202 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V5;
203 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
204 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
205 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
206 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
207 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
208 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
209 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
210 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
211 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
212 #if 0 /* we don't support XPRESS compression yet */
213 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
214 #endif
215 b_state->local_info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V10;
216 b_state->local_info28.site_guid = site_guid;
217 b_state->local_info28.pid = pid;
218 b_state->local_info28.repl_epoch = repl_epoch;
221 * allocate the return bind_info
223 bind_info = talloc_zero(mem_ctx, struct drsuapi_DsBindInfoCtr);
224 W_ERROR_HAVE_NO_MEMORY(bind_info);
226 bind_info->length = 28;
227 bind_info->info.info28 = b_state->local_info28;
230 * allocate a bind handle
232 handle = dcesrv_handle_new(dce_call->context, DRSUAPI_BIND_HANDLE);
233 W_ERROR_HAVE_NO_MEMORY(handle);
234 handle->data = talloc_steal(handle, b_state);
237 * prepare reply
239 r->out.bind_info = bind_info;
240 *r->out.bind_handle = handle->wire_handle;
242 return WERR_OK;
247 drsuapi_DsUnbind
249 static WERROR dcesrv_drsuapi_DsUnbind(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
250 struct drsuapi_DsUnbind *r)
252 struct dcesrv_handle *h;
254 *r->out.bind_handle = *r->in.bind_handle;
256 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
258 talloc_free(h);
260 ZERO_STRUCTP(r->out.bind_handle);
262 return WERR_OK;
267 drsuapi_DsReplicaSync
269 static WERROR dcesrv_drsuapi_DsReplicaSync(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
270 struct drsuapi_DsReplicaSync *r)
272 WERROR status;
273 uint32_t timeout;
275 status = drs_security_level_check(dce_call, "DsReplicaSync", SECURITY_DOMAIN_CONTROLLER, NULL);
276 if (!W_ERROR_IS_OK(status)) {
277 return status;
280 if (r->in.level != 1) {
281 DEBUG(0,("DsReplicaSync called with unsupported level %d\n", r->in.level));
282 return WERR_DS_DRA_INVALID_PARAMETER;
285 if (r->in.req->req1.options & DRSUAPI_DRS_ASYNC_OP) {
286 timeout = IRPC_CALL_TIMEOUT;
287 } else {
289 * use Infinite time for timeout in case
290 * the caller made a sync call
292 timeout = IRPC_CALL_TIMEOUT_INF;
295 dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx,
296 r, NDR_DRSUAPI_DSREPLICASYNC,
297 &ndr_table_drsuapi,
298 "dreplsrv", "DsReplicaSync",
299 timeout);
301 return WERR_OK;
306 drsuapi_DsReplicaAdd
308 static WERROR dcesrv_drsuapi_DsReplicaAdd(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
309 struct drsuapi_DsReplicaAdd *r)
311 WERROR status;
313 status = drs_security_level_check(dce_call, "DsReplicaAdd", SECURITY_DOMAIN_CONTROLLER, NULL);
314 if (!W_ERROR_IS_OK(status)) {
315 return status;
318 dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx,
319 r, NDR_DRSUAPI_DSREPLICAADD,
320 &ndr_table_drsuapi,
321 "dreplsrv", "DsReplicaAdd",
322 IRPC_CALL_TIMEOUT);
324 return WERR_OK;
329 drsuapi_DsReplicaDel
331 static WERROR dcesrv_drsuapi_DsReplicaDel(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
332 struct drsuapi_DsReplicaDel *r)
334 WERROR status;
336 status = drs_security_level_check(dce_call, "DsReplicaDel", SECURITY_DOMAIN_CONTROLLER, NULL);
337 if (!W_ERROR_IS_OK(status)) {
338 return status;
341 dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx,
342 r, NDR_DRSUAPI_DSREPLICADEL,
343 &ndr_table_drsuapi,
344 "dreplsrv", "DsReplicaDel",
345 IRPC_CALL_TIMEOUT);
347 return WERR_OK;
352 drsuapi_DsReplicaModify
354 static WERROR dcesrv_drsuapi_DsReplicaMod(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
355 struct drsuapi_DsReplicaMod *r)
357 WERROR status;
359 status = drs_security_level_check(dce_call, "DsReplicaMod", SECURITY_DOMAIN_CONTROLLER, NULL);
360 if (!W_ERROR_IS_OK(status)) {
361 return status;
364 dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx,
365 r, NDR_DRSUAPI_DSREPLICAMOD,
366 &ndr_table_drsuapi,
367 "dreplsrv", "DsReplicaMod",
368 IRPC_CALL_TIMEOUT);
370 return WERR_OK;
375 DRSUAPI_VERIFY_NAMES
377 static WERROR dcesrv_DRSUAPI_VERIFY_NAMES(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
378 struct DRSUAPI_VERIFY_NAMES *r)
380 DRSUAPI_UNSUPPORTED(DRSUAPI_VERIFY_NAMES);
385 drsuapi_DsGetMemberships
387 static WERROR dcesrv_drsuapi_DsGetMemberships(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
388 struct drsuapi_DsGetMemberships *r)
390 DRSUAPI_UNSUPPORTED(drsuapi_DsGetMemberships);
395 DRSUAPI_INTER_DOMAIN_MOVE
397 static WERROR dcesrv_DRSUAPI_INTER_DOMAIN_MOVE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
398 struct DRSUAPI_INTER_DOMAIN_MOVE *r)
400 DRSUAPI_UNSUPPORTED(DRSUAPI_INTER_DOMAIN_MOVE);
405 drsuapi_DsGetNT4ChangeLog
407 static WERROR dcesrv_drsuapi_DsGetNT4ChangeLog(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
408 struct drsuapi_DsGetNT4ChangeLog *r)
410 DRSUAPI_UNSUPPORTED(drsuapi_DsGetNT4ChangeLog);
414 drsuapi_DsCrackNames
416 static WERROR dcesrv_drsuapi_DsCrackNames(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
417 struct drsuapi_DsCrackNames *r)
419 struct drsuapi_bind_state *b_state;
420 struct dcesrv_handle *h;
422 *r->out.level_out = r->in.level;
424 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
425 b_state = h->data;
427 r->out.ctr = talloc_zero(mem_ctx, union drsuapi_DsNameCtr);
428 W_ERROR_HAVE_NO_MEMORY(r->out.ctr);
430 switch (r->in.level) {
431 case 1: {
432 switch(r->in.req->req1.format_offered){
433 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT_NAME_SANS_DOMAIN_EX:
434 case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT_NAME_SANS_DOMAIN:
435 case DRSUAPI_DS_NAME_FORMAT_STRING_SID_NAME:
436 case DRSUAPI_DS_NAME_FORMAT_ALT_SECURITY_IDENTITIES_NAME:
437 case DRSUAPI_DS_NAME_FORMAT_MAP_SCHEMA_GUID:
438 case DRSUAPI_DS_NAME_FORMAT_LIST_NCS:
439 case DRSUAPI_DS_NAME_FORMAT_LIST_DOMAINS:
440 case DRSUAPI_DS_NAME_FORMAT_LIST_GLOBAL_CATALOG_SERVERS:
441 case DRSUAPI_DS_NAME_FORMAT_LIST_SERVERS_WITH_DCS_IN_SITE:
442 case DRSUAPI_DS_NAME_FORMAT_LIST_SERVERS_FOR_DOMAIN_IN_SITE:
443 case DRSUAPI_DS_NAME_FORMAT_LIST_DOMAINS_IN_SITE:
444 case DRSUAPI_DS_NAME_FORMAT_LIST_SERVERS_IN_SITE:
445 case DRSUAPI_DS_NAME_FORMAT_LIST_SITES:
446 case DRSUAPI_DS_NAME_FORMAT_UPN_AND_ALTSECID:
447 case DRSUAPI_DS_NAME_FORMAT_UPN_FOR_LOGON:
448 DEBUG(0, ("DsCrackNames: Unsupported operation requested: %X",
449 r->in.req->req1.format_offered));
450 return WERR_OK;
451 case DRSUAPI_DS_NAME_FORMAT_LIST_INFO_FOR_SERVER:
452 return dcesrv_drsuapi_ListInfoServer(b_state->sam_ctx, mem_ctx, &r->in.req->req1, &r->out.ctr->ctr1);
453 case DRSUAPI_DS_NAME_FORMAT_LIST_ROLES:
454 return dcesrv_drsuapi_ListRoles(b_state->sam_ctx, mem_ctx,
455 &r->in.req->req1, &r->out.ctr->ctr1);
456 default:/* format_offered is in the enum drsuapi_DsNameFormat*/
457 return dcesrv_drsuapi_CrackNamesByNameFormat(b_state->sam_ctx, mem_ctx,
458 &r->in.req->req1, &r->out.ctr->ctr1);
462 return WERR_UNKNOWN_LEVEL;
467 drsuapi_DsRemoveDSServer
469 static WERROR dcesrv_drsuapi_DsRemoveDSServer(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
470 struct drsuapi_DsRemoveDSServer *r)
472 struct drsuapi_bind_state *b_state;
473 struct dcesrv_handle *h;
474 struct ldb_dn *ntds_dn;
475 int ret;
476 bool ok;
477 WERROR status;
479 *r->out.level_out = 1;
481 status = drs_security_level_check(dce_call, "DsRemoveDSServer", SECURITY_DOMAIN_CONTROLLER, NULL);
482 if (!W_ERROR_IS_OK(status)) {
483 return status;
486 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
487 b_state = h->data;
489 switch (r->in.level) {
490 case 1:
491 ntds_dn = ldb_dn_new(mem_ctx, b_state->sam_ctx, r->in.req->req1.server_dn);
492 W_ERROR_HAVE_NO_MEMORY(ntds_dn);
494 ok = ldb_dn_validate(ntds_dn);
495 if (!ok) {
496 return WERR_FOOBAR;
499 /* TODO: it's likely that we need more checks here */
501 ok = ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings");
502 if (!ok) {
503 return WERR_FOOBAR;
506 if (r->in.req->req1.commit) {
507 ret = dsdb_delete(b_state->sam_ctx, ntds_dn, DSDB_TREE_DELETE);
508 if (ret != LDB_SUCCESS) {
509 return WERR_FOOBAR;
513 return WERR_OK;
514 default:
515 break;
518 return WERR_FOOBAR;
523 DRSUAPI_REMOVE_DS_DOMAIN
525 static WERROR dcesrv_DRSUAPI_REMOVE_DS_DOMAIN(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
526 struct DRSUAPI_REMOVE_DS_DOMAIN *r)
528 DRSUAPI_UNSUPPORTED(DRSUAPI_REMOVE_DS_DOMAIN);
531 /* Obtain the site name from a server DN */
532 static const char *result_site_name(struct ldb_dn *server_dn)
534 /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
535 const struct ldb_val *val = ldb_dn_get_component_val(server_dn, 2);
536 const char *name = ldb_dn_get_component_name(server_dn, 2);
538 if (!name || (ldb_attr_cmp(name, "cn") != 0)) {
539 /* Ensure this matches the format. This gives us a
540 * bit more confidence that a 'cn' value will be a
541 * ascii string */
542 return NULL;
544 if (val) {
545 return (char *)val->data;
547 return NULL;
551 drsuapi_DsGetDomainControllerInfo
553 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo_1(struct drsuapi_bind_state *b_state,
554 TALLOC_CTX *mem_ctx,
555 struct drsuapi_DsGetDomainControllerInfo *r)
557 struct ldb_dn *sites_dn;
558 struct ldb_result *res;
560 const char *attrs_account_1[] = { "cn", "dnsHostName", NULL };
561 const char *attrs_account_2[] = { "cn", "dnsHostName", "objectGUID", NULL };
563 const char *attrs_none[] = { NULL };
565 const char *attrs_site[] = { "objectGUID", NULL };
567 const char *attrs_ntds[] = { "options", "objectGUID", NULL };
569 const char *attrs_1[] = { "serverReference", "cn", "dnsHostName", NULL };
570 const char *attrs_2[] = { "serverReference", "cn", "dnsHostName", "objectGUID", NULL };
571 const char **attrs;
573 struct drsuapi_DsGetDCInfoCtr1 *ctr1;
574 struct drsuapi_DsGetDCInfoCtr2 *ctr2;
576 int ret;
577 unsigned int i;
579 *r->out.level_out = r->in.req->req1.level;
580 r->out.ctr = talloc_zero(mem_ctx, union drsuapi_DsGetDCInfoCtr);
581 W_ERROR_HAVE_NO_MEMORY(r->out.ctr);
583 switch (*r->out.level_out) {
584 case -1:
585 /* this level is not like the others */
586 return WERR_UNKNOWN_LEVEL;
587 case 1:
588 attrs = attrs_1;
589 break;
590 case 2:
591 attrs = attrs_2;
592 break;
593 default:
594 return WERR_UNKNOWN_LEVEL;
597 sites_dn = samdb_sites_dn(b_state->sam_ctx, mem_ctx);
598 if (!sites_dn) {
599 return WERR_DS_OBJ_NOT_FOUND;
602 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res, sites_dn, LDB_SCOPE_SUBTREE, attrs,
603 "(&(objectClass=server)(serverReference=*))");
605 if (ret) {
606 DEBUG(1, ("searching for servers in sites DN %s failed: %s\n",
607 ldb_dn_get_linearized(sites_dn), ldb_errstring(b_state->sam_ctx)));
608 return WERR_GENERAL_FAILURE;
611 switch (*r->out.level_out) {
612 case 1:
613 ctr1 = &r->out.ctr->ctr1;
614 ctr1->count = res->count;
615 ctr1->array = talloc_zero_array(mem_ctx,
616 struct drsuapi_DsGetDCInfo1,
617 res->count);
618 for (i=0; i < res->count; i++) {
619 struct ldb_dn *domain_dn;
620 struct ldb_result *res_domain;
621 struct ldb_result *res_account;
622 struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
624 struct ldb_dn *ref_dn
625 = ldb_msg_find_attr_as_dn(b_state->sam_ctx,
626 mem_ctx, res->msgs[i],
627 "serverReference");
629 if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
630 return WERR_NOMEM;
633 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
634 LDB_SCOPE_BASE, attrs_account_1,
635 "(&(objectClass=computer)(userAccountControl:1.2.840.113556.1.4.803:=%u))",
636 UF_SERVER_TRUST_ACCOUNT);
637 if (ret == LDB_SUCCESS && res_account->count == 1) {
638 const char *errstr;
639 ctr1->array[i].dns_name
640 = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
641 ctr1->array[i].netbios_name
642 = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
643 ctr1->array[i].computer_dn
644 = ldb_dn_get_linearized(res_account->msgs[0]->dn);
646 /* Determine if this is the PDC */
647 ret = samdb_search_for_parent_domain(b_state->sam_ctx,
648 mem_ctx, res_account->msgs[0]->dn,
649 &domain_dn, &errstr);
651 if (ret == LDB_SUCCESS) {
652 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
653 LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
654 ldb_dn_get_linearized(ntds_dn));
655 if (ret) {
656 return WERR_GENERAL_FAILURE;
658 if (res_domain->count == 1) {
659 ctr1->array[i].is_pdc = true;
663 if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
664 DEBUG(5, ("warning: searching for computer DN %s failed: %s\n",
665 ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
668 /* Look at server DN and extract site component */
669 ctr1->array[i].site_name = result_site_name(res->msgs[i]->dn);
670 ctr1->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
673 ctr1->array[i].is_enabled = true;
676 break;
677 case 2:
678 ctr2 = &r->out.ctr->ctr2;
679 ctr2->count = res->count;
680 ctr2->array = talloc_zero_array(mem_ctx,
681 struct drsuapi_DsGetDCInfo2,
682 res->count);
683 for (i=0; i < res->count; i++) {
684 struct ldb_dn *domain_dn;
685 struct ldb_result *res_domain;
686 struct ldb_result *res_account;
687 struct ldb_dn *ntds_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
688 struct ldb_result *res_ntds;
689 struct ldb_dn *site_dn = ldb_dn_copy(mem_ctx, res->msgs[i]->dn);
690 struct ldb_result *res_site;
691 struct ldb_dn *ref_dn
692 = ldb_msg_find_attr_as_dn(b_state->sam_ctx,
693 mem_ctx, res->msgs[i],
694 "serverReference");
696 if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
697 return WERR_NOMEM;
700 /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
701 if (!site_dn || !ldb_dn_remove_child_components(site_dn, 2)) {
702 return WERR_NOMEM;
705 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_ntds, ntds_dn,
706 LDB_SCOPE_BASE, attrs_ntds, "objectClass=nTDSDSA");
707 if (ret == LDB_SUCCESS && res_ntds->count == 1) {
708 ctr2->array[i].is_gc
709 = (ldb_msg_find_attr_as_uint(res_ntds->msgs[0], "options", 0) & DS_NTDSDSA_OPT_IS_GC);
710 ctr2->array[i].ntds_guid
711 = samdb_result_guid(res_ntds->msgs[0], "objectGUID");
712 ctr2->array[i].ntds_dn = ldb_dn_get_linearized(res_ntds->msgs[0]->dn);
714 if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
715 DEBUG(5, ("warning: searching for NTDS DN %s failed: %s\n",
716 ldb_dn_get_linearized(ntds_dn), ldb_errstring(b_state->sam_ctx)));
719 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_site, site_dn,
720 LDB_SCOPE_BASE, attrs_site, "objectClass=site");
721 if (ret == LDB_SUCCESS && res_site->count == 1) {
722 ctr2->array[i].site_guid
723 = samdb_result_guid(res_site->msgs[0], "objectGUID");
724 ctr2->array[i].site_dn = ldb_dn_get_linearized(res_site->msgs[0]->dn);
726 if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
727 DEBUG(5, ("warning: searching for site DN %s failed: %s\n",
728 ldb_dn_get_linearized(site_dn), ldb_errstring(b_state->sam_ctx)));
731 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
732 LDB_SCOPE_BASE, attrs_account_2, "objectClass=computer");
733 if (ret == LDB_SUCCESS && res_account->count == 1) {
734 const char *errstr;
735 ctr2->array[i].dns_name
736 = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
737 ctr2->array[i].netbios_name
738 = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
739 ctr2->array[i].computer_dn = ldb_dn_get_linearized(res_account->msgs[0]->dn);
740 ctr2->array[i].computer_guid
741 = samdb_result_guid(res_account->msgs[0], "objectGUID");
743 /* Determine if this is the PDC */
744 ret = samdb_search_for_parent_domain(b_state->sam_ctx,
745 mem_ctx, res_account->msgs[0]->dn,
746 &domain_dn, &errstr);
748 if (ret == LDB_SUCCESS) {
749 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
750 LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
751 ldb_dn_get_linearized(ntds_dn));
752 if (ret == LDB_SUCCESS && res_domain->count == 1) {
753 ctr2->array[i].is_pdc = true;
755 if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
756 DEBUG(5, ("warning: searching for domain DN %s failed: %s\n",
757 ldb_dn_get_linearized(domain_dn), ldb_errstring(b_state->sam_ctx)));
761 if ((ret != LDB_SUCCESS) && (ret != LDB_ERR_NO_SUCH_OBJECT)) {
762 DEBUG(5, ("warning: searching for computer account DN %s failed: %s\n",
763 ldb_dn_get_linearized(ref_dn), ldb_errstring(b_state->sam_ctx)));
766 /* Look at server DN and extract site component */
767 ctr2->array[i].site_name = result_site_name(res->msgs[i]->dn);
768 ctr2->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
769 ctr2->array[i].server_guid
770 = samdb_result_guid(res->msgs[i], "objectGUID");
772 ctr2->array[i].is_enabled = true;
775 break;
777 return WERR_OK;
781 drsuapi_DsGetDomainControllerInfo
783 static WERROR dcesrv_drsuapi_DsGetDomainControllerInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
784 struct drsuapi_DsGetDomainControllerInfo *r)
786 struct dcesrv_handle *h;
787 struct drsuapi_bind_state *b_state;
788 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
789 b_state = h->data;
791 switch (r->in.level) {
792 case 1:
793 return dcesrv_drsuapi_DsGetDomainControllerInfo_1(b_state, mem_ctx, r);
796 return WERR_UNKNOWN_LEVEL;
802 drsuapi_DsExecuteKCC
804 static WERROR dcesrv_drsuapi_DsExecuteKCC(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
805 struct drsuapi_DsExecuteKCC *r)
807 WERROR status;
808 status = drs_security_level_check(dce_call, "DsExecuteKCC", SECURITY_DOMAIN_CONTROLLER, NULL);
810 if (!W_ERROR_IS_OK(status)) {
811 return status;
813 if (r->in.req->ctr1.taskID != 0) {
814 return WERR_INVALID_PARAM;
816 dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSEXECUTEKCC,
817 &ndr_table_drsuapi, "kccsrv", "DsExecuteKCC",
818 IRPC_CALL_TIMEOUT);
819 return WERR_OK;
824 drsuapi_DsReplicaGetInfo
826 static WERROR dcesrv_drsuapi_DsReplicaGetInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
827 struct drsuapi_DsReplicaGetInfo *r)
829 enum security_user_level level;
831 if (!lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx, NULL,
832 "drs", "disable_sec_check", false)) {
833 level = security_session_user_level(dce_call->conn->auth_state.session_info, NULL);
834 if (level < SECURITY_DOMAIN_CONTROLLER) {
835 DEBUG(1,(__location__ ": Administrator access required for DsReplicaGetInfo\n"));
836 security_token_debug(0, 2, dce_call->conn->auth_state.session_info->security_token);
837 return WERR_DS_DRA_ACCESS_DENIED;
841 dcesrv_irpc_forward_rpc_call(dce_call, mem_ctx, r, NDR_DRSUAPI_DSREPLICAGETINFO,
842 &ndr_table_drsuapi, "kccsrv", "DsReplicaGetInfo",
843 IRPC_CALL_TIMEOUT);
845 return WERR_OK;
850 DRSUAPI_ADD_SID_HISTORY
852 static WERROR dcesrv_DRSUAPI_ADD_SID_HISTORY(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
853 struct DRSUAPI_ADD_SID_HISTORY *r)
855 DRSUAPI_UNSUPPORTED(DRSUAPI_ADD_SID_HISTORY);
859 drsuapi_DsGetMemberships2
861 static WERROR dcesrv_drsuapi_DsGetMemberships2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
862 struct drsuapi_DsGetMemberships2 *r)
864 DRSUAPI_UNSUPPORTED(drsuapi_DsGetMemberships2);
868 DRSUAPI_REPLICA_VERIFY_OBJECTS
870 static WERROR dcesrv_DRSUAPI_REPLICA_VERIFY_OBJECTS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
871 struct DRSUAPI_REPLICA_VERIFY_OBJECTS *r)
873 DRSUAPI_UNSUPPORTED(DRSUAPI_REPLICA_VERIFY_OBJECTS);
878 DRSUAPI_GET_OBJECT_EXISTENCE
880 static WERROR dcesrv_DRSUAPI_GET_OBJECT_EXISTENCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
881 struct DRSUAPI_GET_OBJECT_EXISTENCE *r)
883 DRSUAPI_UNSUPPORTED(DRSUAPI_GET_OBJECT_EXISTENCE);
888 drsuapi_QuerySitesByCost
890 static WERROR dcesrv_drsuapi_QuerySitesByCost(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
891 struct drsuapi_QuerySitesByCost *r)
893 DRSUAPI_UNSUPPORTED(drsuapi_QuerySitesByCost);
897 /* include the generated boilerplate */
898 #include "librpc/gen_ndr/ndr_drsuapi_s.c"