2 Unix SMB/CIFS mplementation.
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/>.
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
;
43 return LDB_ERR_OPERATIONS_ERROR
;
46 msg
= ldb_msg_new(mem_ctx
);
49 return LDB_ERR_OPERATIONS_ERROR
;
51 msg_idx
= ldb_msg_new(mem_ctx
);
54 return LDB_ERR_OPERATIONS_ERROR
;
56 msg
->dn
= ldb_dn_new(msg
, ldb
, "@ATTRIBUTES");
59 return LDB_ERR_OPERATIONS_ERROR
;
61 msg_idx
->dn
= ldb_dn_new(msg
, ldb
, "@INDEXLIST");
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
;
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
) {
84 if (attr
->searchFlags
& SEARCH_FLAG_ATTINDEX
) {
85 ret
= ldb_msg_add_string(msg_idx
, "@IDXATTR", attr
->lDAPDisplayName
);
86 if (ret
!= LDB_SUCCESS
) {
95 ret
= ldb_schema_attribute_add(ldb
, attr
->lDAPDisplayName
, LDB_ATTR_FLAG_FIXED
,
97 if (ret
!= LDB_SUCCESS
) {
98 s
= ldb_samba_syntax_by_name(ldb
, attr
->syntax
->ldap_oid
);
100 ret
= ldb_schema_attribute_add_with_syntax(ldb
, attr
->lDAPDisplayName
, LDB_ATTR_FLAG_FIXED
, s
);
102 ret
= LDB_SUCCESS
; /* Nothing to do here */
106 if (ret
!= LDB_SUCCESS
) {
111 if (!write_attributes
|| ret
!= LDB_SUCCESS
) {
112 talloc_free(mem_ctx
);
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
);
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 */
139 if (ret
!= LDB_SUCCESS
) {
140 talloc_free(mem_ctx
);
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
);
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 */
166 talloc_free(mem_ctx
);
172 * Attach the schema to an opaque pointer on the ldb, so ldb modules
176 int dsdb_set_schema(struct ldb_context
*ldb
, struct dsdb_schema
*schema
)
180 ret
= ldb_set_opaque(ldb
, "dsdb_schema", schema
);
181 if (ret
!= LDB_SUCCESS
) {
185 /* Set the new attributes based on the new schema */
186 ret
= dsdb_schema_set_attributes(ldb
, schema
, true);
187 if (ret
!= LDB_SUCCESS
) {
191 talloc_steal(ldb
, schema
);
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
)
207 if (!global_schema
) {
210 ret
= ldb_set_opaque(ldb
, "dsdb_schema", global_schema
);
211 if (ret
!= LDB_SUCCESS
) {
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
) {
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
;
230 * Find the schema object for this ldb
233 struct dsdb_schema
*dsdb_get_schema(struct ldb_context
*ldb
)
236 struct dsdb_schema
*schema
;
238 /* see if we have a cached copy */
239 p
= ldb_get_opaque(ldb
, "dsdb_schema");
244 schema
= talloc_get_type(p
, struct dsdb_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
);
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
;
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
);
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
) {
306 * load the prefixMap attribute from pf
308 ldif
= ldb_ldif_read_string(ldb
, &pf
);
310 status
= WERR_INVALID_PARAM
;
313 talloc_steal(mem_ctx
, ldif
);
315 msg
= ldb_msg_canonicalize(ldb
, ldif
->msg
);
319 talloc_steal(mem_ctx
, msg
);
322 prefix_val
= ldb_msg_find_ldb_val(msg
, "prefixMap");
324 status
= WERR_INVALID_PARAM
;
328 info_val
= ldb_msg_find_ldb_val(msg
, "schemaInfo");
330 info_val_default
= strhex_to_data_blob(mem_ctx
, "FF0000000000000000000000000000000000000000");
331 if (!info_val_default
.data
) {
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
)) {
343 * load the attribute and class definitions outof df
345 while ((ldif
= ldb_ldif_read_string(ldb
, &df
))) {
349 talloc_steal(mem_ctx
, ldif
);
351 msg
= ldb_msg_canonicalize(ldb
, ldif
->msg
);
356 talloc_steal(mem_ctx
, msg
);
359 is_sa
= ldb_msg_check_string_attribute(msg
, "objectClass", "attributeSchema");
360 is_sc
= ldb_msg_check_string_attribute(msg
, "objectClass", "classSchema");
363 struct dsdb_attribute
*sa
;
365 sa
= talloc_zero(schema
, struct dsdb_attribute
);
370 status
= dsdb_attribute_from_ldb(schema
, msg
, sa
, sa
);
371 if (!W_ERROR_IS_OK(status
)) {
375 DLIST_ADD_END(schema
->attributes
, sa
, struct dsdb_attribute
*);
377 struct dsdb_class
*sc
;
379 sc
= talloc_zero(schema
, struct dsdb_class
);
384 status
= dsdb_class_from_ldb(schema
, msg
, sc
, sc
);
385 if (!W_ERROR_IS_OK(status
)) {
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
;
405 talloc_free(mem_ctx
);