r24247: Remove extra newlines from ldb_debug() calls - it already adds one.
[ira/wip.git] / source4 / dsdb / samdb / ldb_modules / schema_fsmo.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    The module that handles the Schema FSMO Role Owner
5    checkings, it also loads the dsdb_schema.
6    
7    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
8     
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.
13    
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.
18    
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/>.
21    
22 */
23
24 #include "includes.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "lib/ldb/include/ldb_private.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "librpc/gen_ndr/ndr_misc.h"
30 #include "librpc/gen_ndr/ndr_drsuapi.h"
31 #include "librpc/gen_ndr/ndr_drsblobs.h"
32 #include "lib/util/dlinklist.h"
33
34 static int schema_fsmo_init(struct ldb_module *module)
35 {
36         WERROR status;
37         TALLOC_CTX *mem_ctx;
38         struct ldb_dn *schema_dn;
39         struct dsdb_schema *schema;
40         struct dsdb_schema_fsmo *schema_fsmo;
41         struct ldb_result *schema_res;
42         const struct ldb_val *prefix_val;
43         const struct ldb_val *info_val;
44         struct ldb_val info_val_default;
45         struct ldb_result *a_res;
46         struct ldb_result *c_res;
47         uint32_t i;
48         int ret;
49         static const char *schema_attrs[] = {
50                 "prefixMap",
51                 "schemaInfo",
52                 "fSMORoleOwner",
53                 NULL
54         };
55
56         if (dsdb_get_schema(module->ldb)) {
57           return ldb_next_init(module);
58         }
59
60         schema_dn = samdb_schema_dn(module->ldb);
61         if (!schema_dn) {
62                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
63                           "schema_fsmo_init: no schema dn present: (skip schema loading)");
64                 return ldb_next_init(module);
65         }
66
67         mem_ctx = talloc_new(module);
68         if (!mem_ctx) {
69                 ldb_oom(module->ldb);
70                 return LDB_ERR_OPERATIONS_ERROR;
71         }
72
73         schema_fsmo = talloc_zero(mem_ctx, struct dsdb_schema_fsmo);
74         if (!schema_fsmo) {
75                 ldb_oom(module->ldb);
76                 return LDB_ERR_OPERATIONS_ERROR;
77         }
78         module->private_data = schema_fsmo;
79
80         schema = talloc_zero(mem_ctx, struct dsdb_schema);
81         if (!schema) {
82                 ldb_oom(module->ldb);
83                 return LDB_ERR_OPERATIONS_ERROR;
84         }
85
86         /*
87          * setup the prefix mappings and schema info
88          */
89         ret = ldb_search(module->ldb, schema_dn,
90                          LDB_SCOPE_BASE,
91                          NULL, schema_attrs,
92                          &schema_res);
93         if (ret != LDB_SUCCESS) {
94                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
95                               "schema_fsmo_init: failed to search the schema head: %d:%s",
96                               ret, ldb_strerror(ret));
97                 talloc_free(mem_ctx);
98                 return ret;
99         }
100         talloc_steal(mem_ctx, schema_res);
101         if (schema_res->count == 0) {
102                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
103                           "schema_fsmo_init: no schema head present: (skip schema loading)");
104                 talloc_free(mem_ctx);
105                 return ldb_next_init(module);
106         } else if (schema_res->count > 1) {
107                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
108                               "schema_fsmo_init: [%u] schema heads found on a base search",
109                               schema_res->count);
110                 talloc_free(mem_ctx);
111                 return LDB_ERR_CONSTRAINT_VIOLATION;
112         }
113
114         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
115         if (!prefix_val) {
116                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
117                               "schema_fsmo_init: no prefixMap attribute found");
118                 talloc_free(mem_ctx);
119                 return LDB_ERR_CONSTRAINT_VIOLATION;
120         }
121         info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
122         if (!info_val) {
123                 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
124                 if (!info_val_default.data) {
125                         ldb_oom(module->ldb);
126                         return LDB_ERR_OPERATIONS_ERROR;
127                 }
128                 talloc_steal(mem_ctx, info_val_default.data);
129                 info_val = &info_val_default;
130         }
131
132         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
133         if (!W_ERROR_IS_OK(status)) {
134                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
135                               "schema_fsmo_init: failed to load oid mappings: %s",
136                               win_errstr(status));
137                 talloc_free(mem_ctx);
138                 return LDB_ERR_CONSTRAINT_VIOLATION;
139         }
140
141         /*
142          * load the attribute definitions
143          */
144         ret = ldb_search(module->ldb, schema_dn,
145                          LDB_SCOPE_ONELEVEL,
146                          "(objectClass=attributeSchema)", NULL,
147                          &a_res);
148         if (ret != LDB_SUCCESS) {
149                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
150                               "schema_fsmo_init: failed to search attributeSchema objects: %d:%s",
151                               ret, ldb_strerror(ret));
152                 talloc_free(mem_ctx);
153                 return ret;
154         }
155         talloc_steal(mem_ctx, a_res);
156
157         for (i=0; i < a_res->count; i++) {
158                 struct dsdb_attribute *sa;
159
160                 sa = talloc_zero(schema, struct dsdb_attribute);
161                 if (!sa) {
162                         ldb_oom(module->ldb);
163                         return LDB_ERR_OPERATIONS_ERROR;
164                 }
165
166                 status = dsdb_attribute_from_ldb(schema, a_res->msgs[i], sa, sa);
167                 if (!W_ERROR_IS_OK(status)) {
168                         ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
169                                       "schema_fsmo_init: failed to load attriute definition: %s:%s",
170                                       ldb_dn_get_linearized(a_res->msgs[i]->dn),
171                                       win_errstr(status));
172                         talloc_free(mem_ctx);
173                         return LDB_ERR_CONSTRAINT_VIOLATION;
174                 }
175
176                 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
177         }
178         talloc_free(a_res);
179
180         /*
181          * load the objectClass definitions
182          */
183         ret = ldb_search(module->ldb, schema_dn,
184                          LDB_SCOPE_ONELEVEL,
185                          "(objectClass=classSchema)", NULL,
186                          &c_res);
187         if (ret != LDB_SUCCESS) {
188                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
189                               "schema_fsmo_init: failed to search classSchema objects: %d:%s",
190                               ret, ldb_strerror(ret));
191                 talloc_free(mem_ctx);
192                 return ret;
193         }
194         talloc_steal(mem_ctx, c_res);
195
196         for (i=0; i < c_res->count; i++) {
197                 struct dsdb_class *sc;
198
199                 sc = talloc_zero(schema, struct dsdb_class);
200                 if (!sc) {
201                         ldb_oom(module->ldb);
202                         return LDB_ERR_OPERATIONS_ERROR;
203                 }
204
205                 status = dsdb_class_from_ldb(schema, c_res->msgs[i], sc, sc);
206                 if (!W_ERROR_IS_OK(status)) {
207                         ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
208                                       "schema_fsmo_init: failed to load class definition: %s:%s",
209                                       ldb_dn_get_linearized(c_res->msgs[i]->dn),
210                                       win_errstr(status));
211                         talloc_free(mem_ctx);
212                         return LDB_ERR_CONSTRAINT_VIOLATION;
213                 }
214
215                 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
216         }
217         talloc_free(c_res);
218
219         /* dsdb_set_schema() steal schema into the ldb_context */
220         ret = dsdb_set_schema(module->ldb, schema);
221         if (ret != LDB_SUCCESS) {
222                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
223                               "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
224                               ret, ldb_strerror(ret));
225                 talloc_free(mem_ctx);
226                 return ret;
227         }
228
229         schema_fsmo->master_dn = ldb_msg_find_attr_as_dn(module->ldb, schema_fsmo, schema_res->msgs[0], "fSMORoleOwner");
230         if (ldb_dn_compare(samdb_ntds_settings_dn(module->ldb), schema_fsmo->master_dn) == 0) {
231                 schema_fsmo->we_are_master = true;
232         } else {
233                 schema_fsmo->we_are_master = false;
234         }
235
236         if (ldb_set_opaque(module->ldb, "dsdb_schema_fsmo", schema_fsmo) != LDB_SUCCESS) {
237                 ldb_oom(module->ldb);
238                 return LDB_ERR_OPERATIONS_ERROR;
239         }
240
241         talloc_steal(module, schema_fsmo);
242
243         ldb_debug(module->ldb, LDB_DEBUG_TRACE,
244                           "schema_fsmo_init: we are master: %s",
245                           (schema_fsmo->we_are_master?"yes":"no"));
246
247         talloc_free(mem_ctx);
248         return ldb_next_init(module);
249 }
250
251 static const struct ldb_module_ops schema_fsmo_ops = {
252         .name           = "schema_fsmo",
253         .init_context   = schema_fsmo_init
254 };
255
256 int schema_fsmo_module_init(void)
257 {
258         return ldb_register_module(&schema_fsmo_ops);
259 }