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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "librpc/gen_ndr/ndr_drsuapi.h"
26 #include "rpc_server/dcerpc_server.h"
27 #include "rpc_server/common/common.h"
28 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
29 #include "dsdb/samdb/samdb.h"
34 static WERROR 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;
42 r->out.bind_info = NULL;
43 ZERO_STRUCTP(r->out.bind_handle);
45 b_state = talloc(dce_call->conn, struct drsuapi_bind_state);
46 W_ERROR_HAVE_NO_MEMORY(b_state);
48 b_state->sam_ctx = samdb_connect(b_state, dce_call->conn->auth_state.session_info);
49 if (!b_state->sam_ctx) {
54 handle = dcesrv_handle_new(dce_call->context, DRSUAPI_BIND_HANDLE);
60 handle->data = talloc_steal(handle, b_state);
62 bind_info = talloc(mem_ctx, struct drsuapi_DsBindInfoCtr);
63 W_ERROR_HAVE_NO_MEMORY(bind_info);
65 ZERO_STRUCT(site_guid);
67 bind_info->length = 28;
68 bind_info->info.info28.supported_extensions = 0;
69 bind_info->info.info28.site_guid = site_guid;
70 bind_info->info.info28.u1 = 0;
71 bind_info->info.info28.repl_epoch = 0;
73 r->out.bind_info = bind_info;
74 *r->out.bind_handle = handle->wire_handle;
83 static WERROR drsuapi_DsUnbind(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
84 struct drsuapi_DsUnbind *r)
86 struct dcesrv_handle *h;
88 *r->out.bind_handle = *r->in.bind_handle;
90 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
94 ZERO_STRUCTP(r->out.bind_handle);
101 drsuapi_DsReplicaSync
103 static WERROR drsuapi_DsReplicaSync(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
104 struct drsuapi_DsReplicaSync *r)
106 /* TODO: implement this call correct!
107 * for now we just say yes,
108 * because we have no output parameter
115 drsuapi_DsGetNCChanges
117 static WERROR drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
118 struct drsuapi_DsGetNCChanges *r)
120 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
125 drsuapi_DsReplicaUpdateRefs
127 static WERROR drsuapi_DsReplicaUpdateRefs(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
128 struct drsuapi_DsReplicaUpdateRefs *r)
130 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
137 static WERROR DRSUAPI_REPLICA_ADD(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
138 struct DRSUAPI_REPLICA_ADD *r)
140 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
147 static WERROR DRSUAPI_REPLICA_DEL(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
148 struct DRSUAPI_REPLICA_DEL *r)
150 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
155 DRSUAPI_REPLICA_MODIFY
157 static WERROR DRSUAPI_REPLICA_MODIFY(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
158 struct DRSUAPI_REPLICA_MODIFY *r)
160 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
167 static WERROR DRSUAPI_VERIFY_NAMES(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
168 struct DRSUAPI_VERIFY_NAMES *r)
170 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
175 drsuapi_DsGetMemberships
177 static WERROR drsuapi_DsGetMemberships(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
178 struct drsuapi_DsGetMemberships *r)
180 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
185 DRSUAPI_INTER_DOMAIN_MOVE
187 static WERROR DRSUAPI_INTER_DOMAIN_MOVE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
188 struct DRSUAPI_INTER_DOMAIN_MOVE *r)
190 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
195 DRSUAPI_GET_NT4_CHANGELOG
197 static WERROR DRSUAPI_GET_NT4_CHANGELOG(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
198 struct DRSUAPI_GET_NT4_CHANGELOG *r)
200 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
207 WERROR drsuapi_DsCrackNames(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
208 struct drsuapi_DsCrackNames *r)
211 struct drsuapi_bind_state *b_state;
212 struct dcesrv_handle *h;
214 r->out.level = r->in.level;
215 ZERO_STRUCT(r->out.ctr);
217 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
220 switch (r->in.level) {
222 struct drsuapi_DsNameCtr1 *ctr1;
223 struct drsuapi_DsNameInfo1 *names;
227 ctr1 = talloc(mem_ctx, struct drsuapi_DsNameCtr1);
228 W_ERROR_HAVE_NO_MEMORY(ctr1);
230 count = r->in.req.req1.count;
231 names = talloc_array(mem_ctx, struct drsuapi_DsNameInfo1, count);
232 W_ERROR_HAVE_NO_MEMORY(names);
234 for (i=0; i < count; i++) {
235 status = DsCrackNameOneName(b_state->sam_ctx, mem_ctx,
236 r->in.req.req1.format_flags,
237 r->in.req.req1.format_offered,
238 r->in.req.req1.format_desired,
239 r->in.req.req1.names[i].str,
241 if (!W_ERROR_IS_OK(status)) {
248 r->out.ctr.ctr1 = ctr1;
254 return WERR_UNKNOWN_LEVEL;
258 drsuapi_DsWriteAccountSpn
260 static WERROR drsuapi_DsWriteAccountSpn(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
261 struct drsuapi_DsWriteAccountSpn *r)
263 struct drsuapi_bind_state *b_state;
264 struct dcesrv_handle *h;
266 r->out.level = r->in.level;
268 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
271 switch (r->in.level) {
273 struct drsuapi_DsWriteAccountSpnRequest1 *req;
274 struct ldb_message *msg;
276 req = &r->in.req.req1;
279 msg = ldb_msg_new(mem_ctx);
284 msg->dn = ldb_dn_new(msg, b_state->sam_ctx, req->object_dn);
285 if ( ! ldb_dn_validate(msg->dn)) {
286 r->out.res.res1.status = WERR_OK;
291 for (i = 0; i < count; i++) {
292 samdb_msg_add_string(b_state->sam_ctx,
293 msg, msg, "servicePrincipalName",
294 req->spn_names[i].str);
296 for (i=0;i<msg->num_elements;i++) {
297 switch (req->operation) {
298 case DRSUAPI_DS_SPN_OPERATION_ADD:
299 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
301 case DRSUAPI_DS_SPN_OPERATION_REPLACE:
302 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
304 case DRSUAPI_DS_SPN_OPERATION_DELETE:
305 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
310 /* Apply to database */
312 ret = samdb_modify(b_state->sam_ctx, mem_ctx, msg);
314 DEBUG(0,("Failed to modify SPNs on %s: %s\n",
315 ldb_dn_get_linearized(msg->dn),
316 ldb_errstring(b_state->sam_ctx)));
317 r->out.res.res1.status = WERR_ACCESS_DENIED;
319 r->out.res.res1.status = WERR_OK;
326 return WERR_UNKNOWN_LEVEL;
331 drsuapi_DsRemoveDSServer
333 static WERROR drsuapi_DsRemoveDSServer(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
334 struct drsuapi_DsRemoveDSServer *r)
336 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
341 DRSUAPI_REMOVE_DS_DOMAIN
343 static WERROR DRSUAPI_REMOVE_DS_DOMAIN(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
344 struct DRSUAPI_REMOVE_DS_DOMAIN *r)
346 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
349 /* Obtain the site name from a server DN */
350 const char *result_site_name(struct ldb_dn *site_dn)
352 /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
353 const struct ldb_val *val = ldb_dn_get_component_val(site_dn, 2);
354 const char *name = ldb_dn_get_component_name(site_dn, 2);
356 if (!name || (ldb_attr_cmp(name, "cn") != 0)) {
357 /* Ensure this matches the format. This gives us a
358 * bit more confidence that a 'cn' value will be a
363 return (char *)val->data;
369 drsuapi_DsGetDomainControllerInfo
371 static WERROR drsuapi_DsGetDomainControllerInfo_1(struct drsuapi_bind_state *b_state,
373 struct drsuapi_DsGetDomainControllerInfo *r)
375 struct ldb_dn *sites_dn;
376 struct ldb_result *res;
378 const char *attrs_account_1[] = { "cn", "dnsHostName", NULL };
379 const char *attrs_account_2[] = { "cn", "dnsHostName", "objectGUID", NULL };
381 const char *attrs_none[] = { NULL };
383 const char *attrs_site[] = { "objectGUID", NULL };
385 const char *attrs_ntds[] = { "options", "objectGUID", NULL };
387 const char *attrs_1[] = { "serverReference", "cn", "dnsHostName", NULL };
388 const char *attrs_2[] = { "serverReference", "cn", "dnsHostName", "objectGUID", NULL };
391 struct drsuapi_DsGetDCInfoCtr1 *ctr1;
392 struct drsuapi_DsGetDCInfoCtr2 *ctr2;
396 r->out.level_out = r->in.req.req1.level;
398 sites_dn = samdb_domain_to_dn(b_state->sam_ctx, mem_ctx, r->in.req.req1.domain_name);
400 return WERR_DS_OBJ_NOT_FOUND;
403 if (!ldb_dn_add_child_fmt(sites_dn, "CN=Sites,CN=Configuration")) {
407 switch (r->out.level_out) {
409 /* this level is not like the others */
410 return WERR_UNKNOWN_LEVEL;
418 return WERR_UNKNOWN_LEVEL;
421 ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res, sites_dn, LDB_SCOPE_SUBTREE, attrs,
422 "objectClass=server");
425 return WERR_GENERAL_FAILURE;
428 switch (r->out.level_out) {
430 ctr1 = &r->out.ctr.ctr1;
431 ctr1->count = res->count;
432 ctr1->array = talloc_zero_array(mem_ctx,
433 struct drsuapi_DsGetDCInfo1,
435 for (i=0; i < res->count; i++) {
436 struct ldb_dn *domain_dn;
437 struct ldb_result *res_domain;
438 struct ldb_result *res_account;
439 struct ldb_dn *ntds_dn = ldb_dn_copy(b_state->sam_ctx, res->msgs[i]->dn);
441 struct ldb_dn *ref_dn
442 = ldb_msg_find_attr_as_dn(b_state->sam_ctx,
443 mem_ctx, res->msgs[i],
446 if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
450 ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
451 LDB_SCOPE_BASE, attrs_account_1, "objectClass=computer");
453 return WERR_GENERAL_FAILURE;
455 if (res_account->count == 1) {
456 ctr1->array[i].dns_name
457 = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
458 ctr1->array[i].netbios_name
459 = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
460 ctr1->array[i].computer_dn
461 = ldb_dn_get_linearized(res_account->msgs[0]->dn);
463 /* Determine if this is the PDC */
464 domain_dn = samdb_search_for_parent_domain(b_state->sam_ctx,
465 mem_ctx, res_account->msgs[0]->dn);
468 ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
469 LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
470 ldb_dn_get_linearized(ntds_dn));
472 return WERR_GENERAL_FAILURE;
474 if (res_domain->count == 1) {
475 ctr1->array[i].is_pdc = True;
480 /* Look at server DN and extract site component */
481 ctr1->array[i].site_name = result_site_name(res->msgs[i]->dn);
482 ctr1->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
485 ctr1->array[i].is_enabled = True;
490 ctr2 = &r->out.ctr.ctr2;
491 ctr2->count = res->count;
492 ctr2->array = talloc_zero_array(mem_ctx,
493 struct drsuapi_DsGetDCInfo2,
495 for (i=0; i < res->count; i++) {
496 struct ldb_dn *domain_dn;
497 struct ldb_result *res_domain;
498 struct ldb_result *res_account;
499 struct ldb_dn *ntds_dn = ldb_dn_copy(b_state->sam_ctx, res->msgs[i]->dn);
500 struct ldb_result *res_ntds;
501 struct ldb_dn *site_dn = ldb_dn_copy(b_state->sam_ctx, res->msgs[i]->dn);
502 struct ldb_result *res_site;
503 struct ldb_dn *ref_dn
504 = ldb_msg_find_attr_as_dn(b_state->sam_ctx,
505 mem_ctx, res->msgs[i],
508 if (!ntds_dn || !ldb_dn_add_child_fmt(ntds_dn, "CN=NTDS Settings")) {
512 /* Format is cn=<NETBIOS name>,cn=Servers,cn=<site>,cn=sites.... */
513 if (!site_dn || !ldb_dn_remove_child_components(site_dn, 2)) {
517 ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res_ntds, ntds_dn,
518 LDB_SCOPE_BASE, attrs_ntds, "objectClass=nTDSDSA");
520 return WERR_GENERAL_FAILURE;
522 if (res_ntds->count == 1) {
524 = (ldb_msg_find_attr_as_int(res_ntds->msgs[0], "options", 0) == 1);
525 ctr2->array[i].ntds_guid
526 = samdb_result_guid(res_ntds->msgs[0], "objectGUID");
527 ctr2->array[i].ntds_dn = ldb_dn_get_linearized(res_ntds->msgs[0]->dn);
530 ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res_site, site_dn,
531 LDB_SCOPE_BASE, attrs_site, "objectClass=site");
533 return WERR_GENERAL_FAILURE;
535 if (res_site->count == 1) {
536 ctr2->array[i].site_guid
537 = samdb_result_guid(res_site->msgs[0], "objectGUID");
538 ctr2->array[i].site_dn = ldb_dn_get_linearized(res_site->msgs[0]->dn);
541 ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res_account, ref_dn,
542 LDB_SCOPE_BASE, attrs_account_2, "objectClass=computer");
544 return WERR_GENERAL_FAILURE;
546 if (res_account->count == 1) {
547 ctr2->array[i].dns_name
548 = ldb_msg_find_attr_as_string(res_account->msgs[0], "dNSHostName", NULL);
549 ctr2->array[i].netbios_name
550 = ldb_msg_find_attr_as_string(res_account->msgs[0], "cn", NULL);
551 ctr2->array[i].computer_dn = ldb_dn_get_linearized(res_account->msgs[0]->dn);
552 ctr2->array[i].computer_guid
553 = samdb_result_guid(res_account->msgs[0], "objectGUID");
555 /* Determine if this is the PDC */
556 domain_dn = samdb_search_for_parent_domain(b_state->sam_ctx,
557 mem_ctx, res_account->msgs[0]->dn);
560 ret = ldb_search_exp_fmt(b_state->sam_ctx, mem_ctx, &res_domain, domain_dn,
561 LDB_SCOPE_BASE, attrs_none, "fSMORoleOwner=%s",
562 ldb_dn_get_linearized(ntds_dn));
564 return WERR_GENERAL_FAILURE;
566 if (res_domain->count == 1) {
567 ctr2->array[i].is_pdc = True;
572 /* Look at server DN and extract site component */
573 ctr2->array[i].site_name = result_site_name(res->msgs[i]->dn);
574 ctr2->array[i].server_dn = ldb_dn_get_linearized(res->msgs[i]->dn);
575 ctr2->array[i].server_guid
576 = samdb_result_guid(res->msgs[i], "objectGUID");
578 ctr2->array[i].is_enabled = True;
587 drsuapi_DsGetDomainControllerInfo
589 static WERROR drsuapi_DsGetDomainControllerInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
590 struct drsuapi_DsGetDomainControllerInfo *r)
592 struct dcesrv_handle *h;
593 struct drsuapi_bind_state *b_state;
594 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
597 switch (r->in.level) {
599 return drsuapi_DsGetDomainControllerInfo_1(b_state, mem_ctx, r);
602 return WERR_UNKNOWN_LEVEL;
609 static WERROR drsuapi_DsAddEntry(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
610 struct drsuapi_DsAddEntry *r)
612 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
619 static WERROR DRSUAPI_EXECUTE_KCC(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
620 struct DRSUAPI_EXECUTE_KCC *r)
622 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
627 drsuapi_DsReplicaGetInfo
629 static WERROR drsuapi_DsReplicaGetInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
630 struct drsuapi_DsReplicaGetInfo *r)
632 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
637 DRSUAPI_ADD_SID_HISTORY
639 static WERROR DRSUAPI_ADD_SID_HISTORY(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
640 struct DRSUAPI_ADD_SID_HISTORY *r)
642 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
646 drsuapi_DsGetMemberships2
648 static WERROR drsuapi_DsGetMemberships2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
649 struct drsuapi_DsGetMemberships2 *r)
651 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
655 DRSUAPI_REPLICA_VERIFY_OBJECTS
657 static WERROR DRSUAPI_REPLICA_VERIFY_OBJECTS(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
658 struct DRSUAPI_REPLICA_VERIFY_OBJECTS *r)
660 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
665 DRSUAPI_GET_OBJECT_EXISTENCE
667 static WERROR DRSUAPI_GET_OBJECT_EXISTENCE(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
668 struct DRSUAPI_GET_OBJECT_EXISTENCE *r)
670 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
675 DRSUAPI_QUERY_SITES_BY_COST
677 static WERROR DRSUAPI_QUERY_SITES_BY_COST(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
678 struct DRSUAPI_QUERY_SITES_BY_COST *r)
680 DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
684 /* include the generated boilerplate */
685 #include "librpc/gen_ndr/ndr_drsuapi_s.c"