Fix formatting.
[ira/wip.git] / source / 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)\n");
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_ERR_NO_SUCH_OBJECT) {
94                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
95                           "schema_fsmo_init: no schema head present: (skip schema loading)\n");
96                 talloc_free(mem_ctx);
97                 return ldb_next_init(module);
98         } else if (ret != LDB_SUCCESS) {
99                 ldb_asprintf_errstring(module->ldb, 
100                                        "schema_fsmo_init: failed to search the schema head: %s",
101                                        ldb_errstring(module->ldb));
102                 talloc_free(mem_ctx);
103                 return ret;
104         }
105         talloc_steal(mem_ctx, schema_res);
106         if (schema_res->count == 0) {
107                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
108                           "schema_fsmo_init: no schema head present: (skip schema loading)\n");
109                 talloc_free(mem_ctx);
110                 return ldb_next_init(module);
111         } else if (schema_res->count > 1) {
112                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
113                               "schema_fsmo_init: [%u] schema heads found on a base search",
114                               schema_res->count);
115                 talloc_free(mem_ctx);
116                 return LDB_ERR_CONSTRAINT_VIOLATION;
117         }
118
119         prefix_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "prefixMap");
120         if (!prefix_val) {
121                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
122                               "schema_fsmo_init: no prefixMap attribute found");
123                 talloc_free(mem_ctx);
124                 return LDB_ERR_CONSTRAINT_VIOLATION;
125         }
126         info_val = ldb_msg_find_ldb_val(schema_res->msgs[0], "schemaInfo");
127         if (!info_val) {
128                 info_val_default = strhex_to_data_blob("FF0000000000000000000000000000000000000000");
129                 if (!info_val_default.data) {
130                         ldb_oom(module->ldb);
131                         return LDB_ERR_OPERATIONS_ERROR;
132                 }
133                 talloc_steal(mem_ctx, info_val_default.data);
134                 info_val = &info_val_default;
135         }
136
137         status = dsdb_load_oid_mappings_ldb(schema, prefix_val, info_val);
138         if (!W_ERROR_IS_OK(status)) {
139                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
140                               "schema_fsmo_init: failed to load oid mappings: %s",
141                               win_errstr(status));
142                 talloc_free(mem_ctx);
143                 return LDB_ERR_CONSTRAINT_VIOLATION;
144         }
145
146         /*
147          * load the attribute definitions
148          */
149         ret = ldb_search(module->ldb, schema_dn,
150                          LDB_SCOPE_ONELEVEL,
151                          "(objectClass=attributeSchema)", NULL,
152                          &a_res);
153         if (ret != LDB_SUCCESS) {
154                 ldb_asprintf_errstring(module->ldb, 
155                                        "schema_fsmo_init: failed to search attributeSchema objects: %s",
156                                        ldb_errstring(module->ldb));
157                 talloc_free(mem_ctx);
158                 return ret;
159         }
160         talloc_steal(mem_ctx, a_res);
161
162         for (i=0; i < a_res->count; i++) {
163                 struct dsdb_attribute *sa;
164
165                 sa = talloc_zero(schema, struct dsdb_attribute);
166                 if (!sa) {
167                         ldb_oom(module->ldb);
168                         return LDB_ERR_OPERATIONS_ERROR;
169                 }
170
171                 status = dsdb_attribute_from_ldb(schema, a_res->msgs[i], sa, sa);
172                 if (!W_ERROR_IS_OK(status)) {
173                         ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
174                                       "schema_fsmo_init: failed to load attriute definition: %s:%s",
175                                       ldb_dn_get_linearized(a_res->msgs[i]->dn),
176                                       win_errstr(status));
177                         talloc_free(mem_ctx);
178                         return LDB_ERR_CONSTRAINT_VIOLATION;
179                 }
180
181                 DLIST_ADD_END(schema->attributes, sa, struct dsdb_attribute *);
182         }
183         talloc_free(a_res);
184
185         /*
186          * load the objectClass definitions
187          */
188         ret = ldb_search(module->ldb, schema_dn,
189                          LDB_SCOPE_ONELEVEL,
190                          "(objectClass=classSchema)", NULL,
191                          &c_res);
192         if (ret != LDB_SUCCESS) {
193                 ldb_asprintf_errstring(module->ldb, 
194                                        "schema_fsmo_init: failed to search classSchema objects: %s",
195                                        ldb_errstring(module->ldb));
196                 talloc_free(mem_ctx);
197                 return ret;
198         }
199         talloc_steal(mem_ctx, c_res);
200
201         for (i=0; i < c_res->count; i++) {
202                 struct dsdb_class *sc;
203
204                 sc = talloc_zero(schema, struct dsdb_class);
205                 if (!sc) {
206                         ldb_oom(module->ldb);
207                         return LDB_ERR_OPERATIONS_ERROR;
208                 }
209
210                 status = dsdb_class_from_ldb(schema, c_res->msgs[i], sc, sc);
211                 if (!W_ERROR_IS_OK(status)) {
212                         ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
213                                       "schema_fsmo_init: failed to load class definition: %s:%s",
214                                       ldb_dn_get_linearized(c_res->msgs[i]->dn),
215                                       win_errstr(status));
216                         talloc_free(mem_ctx);
217                         return LDB_ERR_CONSTRAINT_VIOLATION;
218                 }
219
220                 DLIST_ADD_END(schema->classes, sc, struct dsdb_class *);
221         }
222         talloc_free(c_res);
223
224         /* dsdb_set_schema() steal schema into the ldb_context */
225         ret = dsdb_set_schema(module->ldb, schema);
226         if (ret != LDB_SUCCESS) {
227                 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
228                               "schema_fsmo_init: dsdb_set_schema() failed: %d:%s",
229                               ret, ldb_strerror(ret));
230                 talloc_free(mem_ctx);
231                 return ret;
232         }
233
234         schema_fsmo->master_dn = ldb_msg_find_attr_as_dn(module->ldb, schema_fsmo, schema_res->msgs[0], "fSMORoleOwner");
235         if (ldb_dn_compare(samdb_ntds_settings_dn(module->ldb), schema_fsmo->master_dn) == 0) {
236                 schema_fsmo->we_are_master = true;
237         } else {
238                 schema_fsmo->we_are_master = false;
239         }
240
241         if (ldb_set_opaque(module->ldb, "dsdb_schema_fsmo", schema_fsmo) != LDB_SUCCESS) {
242                 ldb_oom(module->ldb);
243                 return LDB_ERR_OPERATIONS_ERROR;
244         }
245
246         talloc_steal(module, schema_fsmo);
247
248         ldb_debug(module->ldb, LDB_DEBUG_TRACE,
249                           "schema_fsmo_init: we are master: %s\n",
250                           (schema_fsmo->we_are_master?"yes":"no"));
251
252         talloc_free(mem_ctx);
253         return ldb_next_init(module);
254 }
255
256 static const struct ldb_module_ops schema_fsmo_ops = {
257         .name           = "schema_fsmo",
258         .init_context   = schema_fsmo_init
259 };
260
261 int schema_fsmo_module_init(void)
262 {
263         return ldb_register_module(&schema_fsmo_ops);
264 }