s4-drs: fixed the ldap SPN in AddEntry
[Samba/ekacnet.git] / source4 / rpc_server / drsuapi / addentry.c
blobae478027a6fdd9c4a2d1174d0d64915e29af3bfd
1 /*
2 Unix SMB/CIFS implementation.
4 implement the DsAddEntry call
6 Copyright (C) Stefan Metzmacher 2009
7 Copyright (C) Andrew Tridgell 2009
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 "lib/ldb/include/ldb_errors.h"
29 #include "param/param.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "auth/auth.h"
32 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
36 add special SPNs needed for DRS replication to machine accounts when
37 an AddEntry is done to create a nTDSDSA object
39 static WERROR drsuapi_add_SPNs(struct drsuapi_bind_state *b_state,
40 struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
41 const struct drsuapi_DsReplicaObjectListItem *first_object)
43 int ret;
44 const struct drsuapi_DsReplicaObjectListItem *obj;
45 const char *attrs[] = { "serverReference", "objectGUID", NULL };
47 for (obj = first_object; obj; obj=obj->next_object) {
48 const char *dn_string = obj->object.identifier->dn;
49 struct ldb_dn *dn = ldb_dn_new(mem_ctx, b_state->sam_ctx, dn_string);
50 struct ldb_result *res;
51 struct ldb_dn *ref_dn;
52 struct GUID ntds_guid;
53 struct ldb_message *msg;
54 struct ldb_message_element *el;
55 const char *ntds_guid_str;
56 const char *dom_string;
58 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res,
59 dn, LDB_SCOPE_BASE, attrs,
60 "(objectClass=ntDSDSA)");
61 if (ret != LDB_SUCCESS || res->count < 1) {
62 DEBUG(0,(__location__ ": Failed to find dn '%s'\n", dn_string));
63 return WERR_DS_DRA_INTERNAL_ERROR;
66 ref_dn = samdb_result_dn(b_state->sam_ctx, mem_ctx, res->msgs[0], "serverReference", NULL);
67 if (ref_dn == NULL) {
68 /* we only add SPNs for objects with a
69 serverReference */
70 continue;
73 ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
75 ntds_guid_str = GUID_string(res, &ntds_guid);
77 dom_string = lp_realm(dce_call->conn->dce_ctx->lp_ctx);
80 * construct a modify request to add the new SPNs to
81 * the machine account
83 msg = ldb_msg_new(mem_ctx);
84 if (msg == NULL) {
85 return WERR_NOMEM;
88 msg->dn = ref_dn;
89 ret = ldb_msg_add_empty(msg, "servicePrincipalName",
90 LDB_FLAG_MOD_ADD, &el);
91 if (ret != LDB_SUCCESS) {
92 return WERR_NOMEM;
95 el->num_values = 2;
96 el->values = talloc_array(el, struct ldb_val, 2);
97 if (el->values == NULL) {
98 return WERR_NOMEM;
100 /* the magic constant is the GUID of the DRSUAPI RPC
101 interface */
102 el->values[0].data = (uint8_t *)talloc_asprintf(el->values,
103 "E3514235-4B06-11D1-AB04-00C04FC2DCD2/%s/%s",
104 ntds_guid_str, dom_string);
105 el->values[0].length = strlen((char *)el->values[0].data);
106 el->values[1].data = (uint8_t *)talloc_asprintf(el->values, "ldap/%s._msdcs.%s",
107 ntds_guid_str, dom_string);
108 el->values[1].length = strlen((char *)el->values[1].data);
110 ret = ldb_modify(b_state->sam_ctx, msg);
111 if (ret != LDB_SUCCESS) {
112 DEBUG(0,(__location__ ": Failed to add SPNs - %s\n",
113 ldb_errstring(b_state->sam_ctx)));
114 return WERR_DS_DRA_INTERNAL_ERROR;
118 return WERR_OK;
125 drsuapi_DsAddEntry
127 WERROR dcesrv_drsuapi_DsAddEntry(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
128 struct drsuapi_DsAddEntry *r)
130 WERROR status;
131 struct drsuapi_bind_state *b_state;
132 struct dcesrv_handle *h;
133 uint32_t num = 0;
134 struct drsuapi_DsReplicaObjectIdentifier2 *ids = NULL;
135 int ret;
136 const struct drsuapi_DsReplicaObjectListItem *first_object;
138 if (DEBUGLVL(4)) {
139 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsAddEntry, NDR_IN, r);
142 /* TODO: check which out level the client supports */
144 ZERO_STRUCTP(r->out.ctr);
145 *r->out.level_out = 3;
146 r->out.ctr->ctr3.level = 1;
147 r->out.ctr->ctr3.error = talloc_zero(mem_ctx, union drsuapi_DsAddEntryError);
149 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
150 b_state = h->data;
152 switch (r->in.level) {
153 case 2:
154 ret = ldb_transaction_start(b_state->sam_ctx);
155 if (ret != LDB_SUCCESS) {
156 return WERR_DS_DRA_INTERNAL_ERROR;
160 first_object = &r->in.req->req2.first_object;
162 status = dsdb_origin_objects_commit(b_state->sam_ctx,
163 mem_ctx,
164 first_object,
165 &num,
166 &ids);
167 if (!W_ERROR_IS_OK(status)) {
168 r->out.ctr->ctr3.error->info1.status = status;
169 ldb_transaction_cancel(b_state->sam_ctx);
170 return status;
173 r->out.ctr->ctr3.count = num;
174 r->out.ctr->ctr3.objects = ids;
176 break;
177 default:
178 return WERR_FOOBAR;
181 /* if any of the added entries are nTDSDSA objects then we
182 * need to add the SPNs to the machine account
184 status = drsuapi_add_SPNs(b_state, dce_call, mem_ctx, first_object);
185 if (!W_ERROR_IS_OK(status)) {
186 r->out.ctr->ctr3.error->info1.status = status;
187 ldb_transaction_cancel(b_state->sam_ctx);
188 return status;
191 ret = ldb_transaction_commit(b_state->sam_ctx);
192 if (ret != LDB_SUCCESS) {
193 return WERR_DS_DRA_INTERNAL_ERROR;
196 return WERR_OK;