s4:provision: set the correct nTSecurityDescriptor on CN=Builtin,... (bug #9481)
[Samba/gebeck_regimport.git] / source4 / rpc_server / drsuapi / addentry.c
blob9919eae61a06c9983d36dbd6072f1b5583cee41e
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 "rpc_server/dcerpc_server.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "dsdb/common/util.h"
27 #include "param/param.h"
28 #include "libcli/security/security.h"
29 #include "libcli/security/session.h"
30 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
31 #include "librpc/gen_ndr/ndr_drsuapi.h"
34 add special SPNs needed for DRS replication to machine accounts when
35 an AddEntry is done to create a nTDSDSA object
37 static WERROR drsuapi_add_SPNs(struct drsuapi_bind_state *b_state,
38 struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
39 const struct drsuapi_DsReplicaObjectListItem *first_object)
41 int ret;
42 const struct drsuapi_DsReplicaObjectListItem *obj;
43 const char *attrs[] = { "serverReference", "objectGUID", NULL };
45 for (obj = first_object; obj; obj=obj->next_object) {
46 const char *dn_string = obj->object.identifier->dn;
47 struct ldb_dn *dn = ldb_dn_new(mem_ctx, b_state->sam_ctx, dn_string);
48 struct ldb_result *res, *res2;
49 struct ldb_dn *ref_dn;
50 struct GUID ntds_guid;
51 struct ldb_message *msg;
52 struct ldb_message_element *el;
53 const char *ntds_guid_str;
54 const char *dom_string;
55 const char *attrs2[] = { "dNSHostName", "cn", NULL };
56 const char *dNSHostName, *cn;
58 DEBUG(6,(__location__ ": Adding SPNs for %s\n",
59 ldb_dn_get_linearized(dn)));
61 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res,
62 dn, LDB_SCOPE_BASE, attrs,
63 "(objectClass=ntDSDSA)");
64 if (ret != LDB_SUCCESS) {
65 DEBUG(0,(__location__ ": Failed to find dn '%s'\n", dn_string));
66 return WERR_DS_DRA_INTERNAL_ERROR;
69 if (res->count < 1) {
70 /* we only add SPNs for nTDSDSA objects */
71 continue;
74 ref_dn = samdb_result_dn(b_state->sam_ctx, mem_ctx, res->msgs[0], "serverReference", NULL);
75 if (ref_dn == NULL) {
76 /* we only add SPNs for objects with a
77 serverReference */
78 continue;
81 DEBUG(6,(__location__ ": serverReference %s\n",
82 ldb_dn_get_linearized(ref_dn)));
84 ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
86 ntds_guid_str = GUID_string(res, &ntds_guid);
88 dom_string = lpcfg_dnsdomain(dce_call->conn->dce_ctx->lp_ctx);
90 /* get the dNSHostName and cn */
91 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res2,
92 ref_dn, LDB_SCOPE_BASE, attrs2, NULL);
93 if (ret != LDB_SUCCESS) {
94 DEBUG(0,(__location__ ": Failed to find ref_dn '%s'\n",
95 ldb_dn_get_linearized(ref_dn)));
96 return WERR_DS_DRA_INTERNAL_ERROR;
99 dNSHostName = ldb_msg_find_attr_as_string(res2->msgs[0], "dNSHostName", NULL);
100 cn = ldb_msg_find_attr_as_string(res2->msgs[0], "cn", NULL);
103 * construct a modify request to add the new SPNs to
104 * the machine account
106 msg = ldb_msg_new(mem_ctx);
107 if (msg == NULL) {
108 return WERR_NOMEM;
111 msg->dn = ref_dn;
112 ret = ldb_msg_add_empty(msg, "servicePrincipalName",
113 LDB_FLAG_MOD_ADD, &el);
114 if (ret != LDB_SUCCESS) {
115 return WERR_NOMEM;
119 ldb_msg_add_steal_string(msg, "servicePrincipalName",
120 talloc_asprintf(el->values,
121 "E3514235-4B06-11D1-AB04-00C04FC2DCD2/%s/%s",
122 ntds_guid_str, dom_string));
123 ldb_msg_add_steal_string(msg, "servicePrincipalName",
124 talloc_asprintf(el->values, "ldap/%s._msdcs.%s",
125 ntds_guid_str, dom_string));
126 if (cn) {
127 ldb_msg_add_steal_string(msg, "servicePrincipalName",
128 talloc_asprintf(el->values, "ldap/%s", cn));
130 if (dNSHostName) {
131 ldb_msg_add_steal_string(msg, "servicePrincipalName",
132 talloc_asprintf(el->values, "ldap/%s", dNSHostName));
134 if (el->num_values < 2) {
135 return WERR_NOMEM;
138 ret = dsdb_modify(b_state->sam_ctx, msg, DSDB_MODIFY_PERMISSIVE);
139 if (ret != LDB_SUCCESS) {
140 DEBUG(0,(__location__ ": Failed to add SPNs - %s\n",
141 ldb_errstring(b_state->sam_ctx)));
142 return WERR_DS_DRA_INTERNAL_ERROR;
146 return WERR_OK;
153 drsuapi_DsAddEntry
155 WERROR dcesrv_drsuapi_DsAddEntry(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
156 struct drsuapi_DsAddEntry *r)
158 WERROR status;
159 struct drsuapi_bind_state *b_state;
160 struct dcesrv_handle *h;
161 uint32_t num = 0;
162 struct drsuapi_DsReplicaObjectIdentifier2 *ids = NULL;
163 int ret;
164 const struct drsuapi_DsReplicaObjectListItem *first_object;
166 if (DEBUGLVL(4)) {
167 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsAddEntry, NDR_IN, r);
170 /* TODO: check which out level the client supports */
172 ZERO_STRUCTP(r->out.ctr);
173 *r->out.level_out = 3;
174 r->out.ctr->ctr3.err_ver = 1;
175 r->out.ctr->ctr3.err_data = talloc_zero(mem_ctx, union drsuapi_DsAddEntry_ErrData);
177 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
178 b_state = h->data;
180 status = drs_security_level_check(dce_call, "DsAddEntry", SECURITY_DOMAIN_CONTROLLER, NULL);
181 if (!W_ERROR_IS_OK(status)) {
182 return status;
185 switch (r->in.level) {
186 case 2:
187 ret = ldb_transaction_start(b_state->sam_ctx);
188 if (ret != LDB_SUCCESS) {
189 return WERR_DS_DRA_INTERNAL_ERROR;
193 first_object = &r->in.req->req2.first_object;
195 status = dsdb_origin_objects_commit(b_state->sam_ctx,
196 mem_ctx,
197 first_object,
198 &num,
199 DSDB_REPL_FLAG_ADD_NCNAME,
200 &ids);
201 if (!W_ERROR_IS_OK(status)) {
202 r->out.ctr->ctr3.err_data->v1.status = status;
203 ldb_transaction_cancel(b_state->sam_ctx);
204 DEBUG(0,(__location__ ": DsAddEntry failed - %s\n", win_errstr(status)));
205 return status;
208 r->out.ctr->ctr3.count = num;
209 r->out.ctr->ctr3.objects = ids;
211 break;
212 default:
213 return WERR_FOOBAR;
216 /* if any of the added entries are nTDSDSA objects then we
217 * need to add the SPNs to the machine account
219 status = drsuapi_add_SPNs(b_state, dce_call, mem_ctx, first_object);
220 if (!W_ERROR_IS_OK(status)) {
221 r->out.ctr->ctr3.err_data->v1.status = status;
222 ldb_transaction_cancel(b_state->sam_ctx);
223 DEBUG(0,(__location__ ": DsAddEntry add SPNs failed - %s\n", win_errstr(status)));
224 return status;
227 ret = ldb_transaction_commit(b_state->sam_ctx);
228 if (ret != LDB_SUCCESS) {
229 DEBUG(0,(__location__ ": DsAddEntry commit failed: %s\n",
230 ldb_errstring(b_state->sam_ctx)));
231 return WERR_DS_DRA_INTERNAL_ERROR;
234 return WERR_OK;