s3-svcctl: Fix invalid buffer memset in _svcctl_QueryServiceObjectSecurity().
[Samba.git] / source4 / dsdb / schema / schema_set.c
blob6abd8a8f883e3f0e49097eeb91b2892b6735c047
1 /*
2 Unix SMB/CIFS mplementation.
3 DSDB schema header
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2006-2007
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "dlinklist.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "lib/ldb/include/ldb_module.h"
27 #include "param/param.h"
30 static int dsdb_schema_set_attributes(struct ldb_context *ldb, struct dsdb_schema *schema, bool write_attributes)
32 int ret = LDB_SUCCESS;
33 struct ldb_result *res;
34 struct ldb_result *res_idx;
35 struct dsdb_attribute *attr;
36 struct ldb_message *mod_msg;
37 TALLOC_CTX *mem_ctx = talloc_new(ldb);
39 struct ldb_message *msg;
40 struct ldb_message *msg_idx;
42 if (!mem_ctx) {
43 return LDB_ERR_OPERATIONS_ERROR;
46 msg = ldb_msg_new(mem_ctx);
47 if (!msg) {
48 ldb_oom(ldb);
49 return LDB_ERR_OPERATIONS_ERROR;
51 msg_idx = ldb_msg_new(mem_ctx);
52 if (!msg_idx) {
53 ldb_oom(ldb);
54 return LDB_ERR_OPERATIONS_ERROR;
56 msg->dn = ldb_dn_new(msg, ldb, "@ATTRIBUTES");
57 if (!msg->dn) {
58 ldb_oom(ldb);
59 return LDB_ERR_OPERATIONS_ERROR;
61 msg_idx->dn = ldb_dn_new(msg, ldb, "@INDEXLIST");
62 if (!msg_idx->dn) {
63 ldb_oom(ldb);
64 return LDB_ERR_OPERATIONS_ERROR;
67 for (attr = schema->attributes; attr; attr = attr->next) {
68 const struct ldb_schema_syntax *s;
69 const char *syntax = attr->syntax->ldb_syntax;
70 if (!syntax) {
71 syntax = attr->syntax->ldap_oid;
74 /* Write out a rough approximation of the schema as an @ATTRIBUTES value, for bootstrapping */
75 if (strcmp(syntax, LDB_SYNTAX_INTEGER) == 0) {
76 ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "INTEGER");
77 } else if (strcmp(syntax, LDB_SYNTAX_DIRECTORY_STRING) == 0) {
78 ret = ldb_msg_add_string(msg, attr->lDAPDisplayName, "CASE_INSENSITIVE");
80 if (ret != LDB_SUCCESS) {
81 break;
84 if (attr->searchFlags & SEARCH_FLAG_ATTINDEX) {
85 ret = ldb_msg_add_string(msg_idx, "@IDXATTR", attr->lDAPDisplayName);
86 if (ret != LDB_SUCCESS) {
87 break;
91 if (!attr->syntax) {
92 continue;
95 ret = ldb_schema_attribute_add(ldb, attr->lDAPDisplayName, LDB_ATTR_FLAG_FIXED,
96 syntax);
97 if (ret != LDB_SUCCESS) {
98 s = ldb_samba_syntax_by_name(ldb, attr->syntax->ldap_oid);
99 if (s) {
100 ret = ldb_schema_attribute_add_with_syntax(ldb, attr->lDAPDisplayName, LDB_ATTR_FLAG_FIXED, s);
101 } else {
102 ret = LDB_SUCCESS; /* Nothing to do here */
106 if (ret != LDB_SUCCESS) {
107 break;
111 if (!write_attributes || ret != LDB_SUCCESS) {
112 talloc_free(mem_ctx);
113 return ret;
117 /* Try to avoid churning the attributes too much - we only want to do this if they have changed */
118 ret = ldb_search(ldb, mem_ctx, &res, msg->dn, LDB_SCOPE_BASE, NULL, "dn=%s", ldb_dn_get_linearized(msg->dn));
119 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
120 ret = ldb_add(ldb, msg);
121 } else if (ret != LDB_SUCCESS) {
122 } else if (res->count != 1) {
123 ret = ldb_add(ldb, msg);
124 } else {
125 ret = LDB_SUCCESS;
126 /* Annoyingly added to our search results */
127 ldb_msg_remove_attr(res->msgs[0], "distinguishedName");
129 mod_msg = ldb_msg_diff(ldb, res->msgs[0], msg);
130 if (mod_msg->num_elements > 0) {
131 ret = ldb_modify(ldb, mod_msg);
135 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
136 /* We might be on a read-only DB */
137 ret = LDB_SUCCESS;
139 if (ret != LDB_SUCCESS) {
140 talloc_free(mem_ctx);
141 return ret;
144 /* Now write out the indexs, as found in the schema (if they have changed) */
146 ret = ldb_search(ldb, mem_ctx, &res_idx, msg_idx->dn, LDB_SCOPE_BASE, NULL, "dn=%s", ldb_dn_get_linearized(msg_idx->dn));
147 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
148 ret = ldb_add(ldb, msg_idx);
149 } else if (ret != LDB_SUCCESS) {
150 } else if (res->count != 1) {
151 ret = ldb_add(ldb, msg_idx);
152 } else {
153 ret = LDB_SUCCESS;
154 /* Annoyingly added to our search results */
155 ldb_msg_remove_attr(res_idx->msgs[0], "distinguishedName");
157 mod_msg = ldb_msg_diff(ldb, res_idx->msgs[0], msg_idx);
158 if (mod_msg->num_elements > 0) {
159 ret = ldb_modify(ldb, mod_msg);
162 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
163 /* We might be on a read-only DB */
164 ret = LDB_SUCCESS;
166 talloc_free(mem_ctx);
167 return ret;
172 * Attach the schema to an opaque pointer on the ldb, so ldb modules
173 * can find it
176 int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
178 int ret;
180 ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
181 if (ret != LDB_SUCCESS) {
182 return ret;
185 /* Set the new attributes based on the new schema */
186 ret = dsdb_schema_set_attributes(ldb, schema, true);
187 if (ret != LDB_SUCCESS) {
188 return ret;
191 talloc_steal(ldb, schema);
193 return LDB_SUCCESS;
197 * Global variable to hold one copy of the schema, used to avoid memory bloat
199 static struct dsdb_schema *global_schema;
202 * Make this ldb use the 'global' schema, setup to avoid having multiple copies in this process
204 int dsdb_set_global_schema(struct ldb_context *ldb)
206 int ret;
207 if (!global_schema) {
208 return LDB_SUCCESS;
210 ret = ldb_set_opaque(ldb, "dsdb_schema", global_schema);
211 if (ret != LDB_SUCCESS) {
212 return ret;
215 /* Set the new attributes based on the new schema */
216 ret = dsdb_schema_set_attributes(ldb, global_schema, false);
217 if (ret != LDB_SUCCESS) {
218 return ret;
221 /* Keep a reference to this schema, just incase the global copy is replaced */
222 if (talloc_reference(ldb, global_schema) == NULL) {
223 return LDB_ERR_OPERATIONS_ERROR;
226 return LDB_SUCCESS;
230 * Find the schema object for this ldb
233 struct dsdb_schema *dsdb_get_schema(struct ldb_context *ldb)
235 const void *p;
236 struct dsdb_schema *schema;
238 /* see if we have a cached copy */
239 p = ldb_get_opaque(ldb, "dsdb_schema");
240 if (!p) {
241 return NULL;
244 schema = talloc_get_type(p, struct dsdb_schema);
245 if (!schema) {
246 return NULL;
249 return schema;
253 * Make the schema found on this ldb the 'global' schema
256 void dsdb_make_schema_global(struct ldb_context *ldb)
258 struct dsdb_schema *schema = dsdb_get_schema(ldb);
259 if (!schema) {
260 return;
263 if (global_schema) {
264 talloc_unlink(talloc_autofree_context(), schema);
267 talloc_steal(talloc_autofree_context(), schema);
268 global_schema = schema;
270 dsdb_set_global_schema(ldb);
275 * Rather than read a schema from the LDB itself, read it from an ldif
276 * file. This allows schema to be loaded and used while adding the
277 * schema itself to the directory.
280 WERROR dsdb_attach_schema_from_ldif_file(struct ldb_context *ldb, const char *pf, const char *df)
282 struct ldb_ldif *ldif;
283 struct ldb_message *msg;
284 TALLOC_CTX *mem_ctx;
285 WERROR status;
286 int ret;
287 struct dsdb_schema *schema;
288 const struct ldb_val *prefix_val;
289 const struct ldb_val *info_val;
290 struct ldb_val info_val_default;
292 mem_ctx = talloc_new(ldb);
293 if (!mem_ctx) {
294 goto nomem;
297 schema = dsdb_new_schema(mem_ctx, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")));
299 schema->fsmo.we_are_master = true;
300 schema->fsmo.master_dn = ldb_dn_new_fmt(schema, ldb, "@PROVISION_SCHEMA_MASTER");
301 if (!schema->fsmo.master_dn) {
302 goto nomem;
306 * load the prefixMap attribute from pf
308 ldif = ldb_ldif_read_string(ldb, &pf);
309 if (!ldif) {
310 status = WERR_INVALID_PARAM;
311 goto failed;
313 talloc_steal(mem_ctx, ldif);
315 msg = ldb_msg_canonicalize(ldb, ldif->msg);
316 if (!msg) {
317 goto nomem;
319 talloc_steal(mem_ctx, msg);
320 talloc_free(ldif);
322 prefix_val = ldb_msg_find_ldb_val(msg, "prefixMap");
323 if (!prefix_val) {
324 status = WERR_INVALID_PARAM;
325 goto failed;
328 info_val = ldb_msg_find_ldb_val(msg, "schemaInfo");
329 if (!info_val) {
330 info_val_default = strhex_to_data_blob(mem_ctx, "FF0000000000000000000000000000000000000000");
331 if (!info_val_default.data) {
332 goto nomem;
334 info_val = &info_val_default;
337 status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
338 if (!W_ERROR_IS_OK(status)) {
339 goto failed;
343 * load the attribute and class definitions outof df
345 while ((ldif = ldb_ldif_read_string(ldb, &df))) {
346 bool is_sa;
347 bool is_sc;
349 talloc_steal(mem_ctx, ldif);
351 msg = ldb_msg_canonicalize(ldb, ldif->msg);
352 if (!msg) {
353 goto nomem;
356 talloc_steal(mem_ctx, msg);
357 talloc_free(ldif);
359 is_sa = ldb_msg_check_string_attribute(msg, "objectClass", "attributeSchema");
360 is_sc = ldb_msg_check_string_attribute(msg, "objectClass", "classSchema");
362 if (is_sa) {
363 struct dsdb_attribute *sa;
365 sa = talloc_zero(schema, struct dsdb_attribute);
366 if (!sa) {
367 goto nomem;
370 status = dsdb_attribute_from_ldb(schema, msg, sa, sa);
371 if (!W_ERROR_IS_OK(status)) {
372 goto failed;
375 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
376 } else if (is_sc) {
377 struct dsdb_class *sc;
379 sc = talloc_zero(schema, struct dsdb_class);
380 if (!sc) {
381 goto nomem;
384 status = dsdb_class_from_ldb(schema, msg, sc, sc);
385 if (!W_ERROR_IS_OK(status)) {
386 goto failed;
389 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
393 ret = dsdb_set_schema(ldb, schema);
394 if (ret != LDB_SUCCESS) {
395 status = WERR_FOOBAR;
396 goto failed;
399 goto done;
401 nomem:
402 status = WERR_NOMEM;
403 failed:
404 done:
405 talloc_free(mem_ctx);
406 return status;