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